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

插件详情v5.1.0

scope

一个依赖注入(控制反转)库。支持嵌套作用域和同类型多个值。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
25.4万
喜欢数
14
Pub 分数
150

安装插件

flutter pub add scope

插件介绍

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

Scope 是一个用于 Flutter 和 Dart 的依赖注入(控制反转)库,可实现结构化、分层的依赖管理。它支持嵌套作用域以隔离依赖项,并允许多个相同类型的实例,非常适合模块化应用。凭借简洁的 API 设计和极少的样板代码,Scope 能够简化应用程序不同部分之间的状态管理和服务实例化。它可在所有主要平台(包括 Android、iOS、Web、macOS、Windows 和 Linux)上无缝运行。

适合场景

  • 模块化应用架构
  • 使用隔离依赖项进行测试
  • 管理共享和作用域服务
  • 多实例服务处理
  • 跨平台应用开发

主要特点

  • 嵌套作用域支持
  • 同一类型的多个值
  • 最小化样板代码
  • Platform-agnostic
  • 干净的 API 设计
  • 与现有代码的轻松集成

适合对象

  • 大规模 Flutter 应用
  • 可测试的架构
  • 微服务类应用模块
  • 需要依赖隔离的团队
  • 开发者避免使用复杂的依赖注入框架

使用建议

  • 新用户的上手难度
  • 与成熟的替代方案相比,文档有限
  • 最适合具有复杂依赖关系树的应用程序

原始 README

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

Scope

Scope provides Inversion of Control using the dependency injection (DI) pattern for Dart applications.

Scope allows you to inject values into a scope and then 'use' those dependencies from any method (or constructor) called within that scope.

Scope is incredibly easy to use with no additional plumbing required.

Scope is not a replacement for the likes of Provider. Provider does dependency injection for your BuildContext whilst Scope provides DI for your call stack.

For Java developers Scope provides similar functionality to thread local variables.

Scope supports nested scopes; overload or add additional values at each level.

Supporters

Scope is supported by OnePub the private Dart package repository.

https://onepub.dev

The best way to understand Scope is with an example:

import 'package:scope/scope.dart';
import 'package:money2/money2.dart';

const ageKey = ScopeKey<int>();
const heightKey = ScopeKey<int>();
const incomeKey = ScopeKey<Money>();
const countKey = ScopeKey<int>();

void main() async {
    /// create a Scope
    final scope = Scope()
    
    /// inject values
    ..value<int>(ageKey, 18)
    ..value<int>(heightKey, 182) // centimetres
    /// single value from factory method
    ..single<Money>(incomeKey, () => calcIncome)
    /// sequence of values from factory method.
    ..sequence<int>(countKey, () => tracker);
    
    /// run some code within the Scope
    await scope.run(() async => a();
}
void a() {
    print('Your height is ${use(heightKey)}');
    printRealWorth();
}

/// `use` the injected values
void printRealWorth() {
    print('You are the ${use(countKey)} person to ask');
    print('You are ${use(ageKey)} years old');
    print('You earn ${use(incomeKey} per hour.');
}

Money calcIncome() {
    final age = use(ageKey);
    if (age < 18) return Money.parse(r'$10.00');

    return Money.parse(r'$20.00');
}

var count = 0;
int tracker()
{
    return count++;
}

See the Scope documentation at:

https://scope.onepub.dev

The best practice guide is worth a read:

https://scope.onepub.dev/guides/best-practice

Authors: 
Philipp Schiffmann <philippschiffmann93@gmail.com>
S. Brett Sutton

Scope is a reimagining of Philipp's zone_id package. All credit goes to Phillipp's original implementation without which Scope wouldn't exist.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目