搜索插件、代码仓与 APP 分类

插件详情v0.2.1

scoped_deps

一个简单的 Dart 库,用于管理基于 dart:async 中 Zones 的作用域依赖项。

AndroidiOSLinuxmacOSWebWindows
近 30 天下载
11.8万
喜欢数
9
Pub 分数
160

安装插件

flutter pub add scoped_deps

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Scoped_deps 是一个轻量级的 Dart 库,使用 dart:async 中的 Zones 管理作用域依赖。它允许开发者在应用的特定部分创建隔离的依赖上下文,确保依赖项仅在预期位置可访问。这通过避免全局状态,促进了更清晰的代码组织和更好的可测试性。该库与所有平台上的 Flutter 应用无缝集成,提供简单的 API 用于在作用域 Zone 内注册和检索依赖。

适合场景

  • 管理UI特定的依赖项
  • 独立测试组件
  • 避免全局状态污染
  • 创建模块化应用部分
  • 简化大型应用中的依赖注入

主要特点

  • 基于区域的依赖作用域
  • 最小 API 表面
  • 跨平台支持
  • 与 Flutter 的轻松集成
  • 无运行时开销

适合对象

  • 寻求轻量级依赖注入的Flutter开发者
  • 具有模块化架构的应用程序
  • 需要清晰依赖边界的团队
  • 测试驱动开发工作流程
  • 避免使用重型框架的项目

使用建议

  • 不适合复杂的依赖关系图
  • 需要理解 Zones
  • 与其他成熟库相比,文档有限
  • 适用于中小型应用
  • 可能需要重构现有的依赖关系模式

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Scoped Deps

A simple dependency injection library built on Zones.

read resolves bindings from an internal registration table that tracks each [Zone] established by this library, so it is not fooled by [Zone]'s inherited [] behavior (which can copy a parent's [ScopedRef] into a middle zone and mask a child's override). For subprocess or other IO that may resume outside that zone, combine with [Zone.bindUnaryCallback] (or related APIs) when registering native callbacks.

Quick Start

import 'package:scoped_deps/scoped_deps.dart';

final value = create(() => 42);

void main() {
  runScoped(scopeA, values: {value});
}

void scopeA() {
  print(read(value)); // 42
  runScoped(scopeB, values: {value.overrideWith(() => 0)});
}

void scopeB() {
  print(read(value)); // 0
}

Recommended: hide read behind a getter

Calling read(valueProvider) at every call site works, but it spreads ScopedRefs through code that shouldn't need to know they exist. Pair each ScopedRef with a top-level getter instead, and let the rest of the app read it like an ordinary variable:

final valueProvider = create(() => 42);
int get value => read(valueProvider);
void main() {
  runScoped(() {
    print(value); // 42
  }, values: {valueProvider});
}

Anything that reads value — no matter how deeply nested — resolves against whichever scope is active when it runs. Callers never import ScopedRef or call read directly, which also makes the pattern straightforward to test (see Testing below).

API

  • create(() => value) — declare a ScopedRef<T>.
  • read(ref, {orElse}) — resolve ref in the current scope; throws a StateError (or calls orElse) if no scope provides it.
  • isRegistered(ref) — check whether ref is bound in the current scope, without the StateError/orElse path.
  • runScoped(body, {values}) — run body in a scope that provides values.
  • runMergedScoped(body, {override, includeIfAbsent}) — run body in a scope forked from the current one. override replaces the current binding for those refs; includeIfAbsent adds a binding only for refs the current scope chain doesn't already define.
  • runMergedScopedFuture(body, {override, includeIfAbsent, zoneSpecification}) — like runMergedScoped, but requires an explicitly async body and accepts an optional zoneSpecification (e.g. to intercept print). override / includeIfAbsent survive await in runMergedScoped too — reach for this variant when you want the stricter async signature or need zoneSpecification, not because bindings would otherwise be lost.
  • runScopedGuarded(body, {onError, values}) — like runScoped, but uncaught errors are routed to onError instead of thrown.
  • ref.overrideWith(() => other) — a ref with the same identity as ref but a different value factory, used to substitute a value for a nested scope (or in a test).

Testing

Bindings live on the Zone, so they don't exist until something establishes a scope — code under test has to run inside a runScoped / runMergedScoped call that provides whatever it reads. A setUp can't do this on its own: it runs to completion before the test body starts, so any scope it forks is already gone by the time your test executes. Wrap the test body itself:

final valueProvider = create(() => 42);
int get value => read(valueProvider);

int doubleValue() => value * 2;

test('doubleValue uses the overridden value', () {
  runScoped(() {
    expect(doubleValue(), equals(20));
  }, values: {valueProvider.overrideWith(() => 10)});
});

The override applies no matter how deep the call chain is — doubleValue() never has to know it's under test. The same works for async code under test; the override stays bound across every await:

Future<int> asyncDoubleValue() async {
  await Future<void>.delayed(Duration.zero);
  return value * 2;
}

test('asyncDoubleValue uses the overridden value', () async {
  await runScoped(() async {
    expect(await asyncDoubleValue(), equals(20));
  }, values: {valueProvider.overrideWith(() => 10)});
});

Reach for runMergedScopedFuture instead when a test also needs a zoneSpecification — for example, to capture print output for the duration of the test rather than letting it hit the console.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。

所在榜单