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

插件详情v1.0.1

bloc_provider

为子级组件提供 bloc(O(1)),并且 bloc 会根据 bloc_provider 内部持有的状态进行适当释放。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
5000
喜欢数
25
Pub 分数
150

安装插件

flutter pub add bloc_provider

插件介绍

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

BlocProvider 是一个轻量级包,能够以 O(1) 的查找时间高效地将 Bloc 实例传递给 Flutter 应用中的子级组件。它会管理所提供 Bloc 的生命周期,在拥有该 Bloc 的组件从树中移除时自动进行清理。专为与 Bloc 模式无缝集成而设计,通过确保正确的资源清理并减少样板代码,简化了状态管理。适用于在多个嵌套组件中使用 Bloc 进行状态处理的应用。

适合场景

  • 向子部件提供 Bloc 实例
  • 在复杂的 widget 树中管理 Bloc 生命周期
  • 简化嵌套 UI 组件中的状态访问
  • 实现用户界面与业务逻辑之间的清晰分离
  • 在大型应用中支持多级状态共享

主要特点

  • 通过 widget 树实现 O(1) 的 Bloc 访问
  • 在移除时自动释放 Bloc
  • 最小化样板代码
  • 内置生命周期管理
  • 适用于任何 Bloc 实现

适合对象

  • 使用 Bloc 模式的 Flutter 应用
  • 寻求高效状态传播的开发者
  • 需要自动资源清理的项目
  • 具有深层小部件层次结构的大规模应用
  • 优先考虑干净状态管理架构的团队

使用建议

  • 需要理解 Bloc 模式
  • 不适用于非Bloc状态管理
  • 如果在浅层树中过度使用,可能会引入开销
  • 依赖于正确的控件树结构进行释放
  • 仅限Bloc作为主要状态容器的使用场景

原始 README

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

bloc_provider Codemagic build status

Provides BLoC(Business Logic Component) to descendant widget (O(1)), and the bloc is disposed automatically by the state which the bloc_provider holds internally.

Recommended other packages

bloc_provider was one of the good choice for BLoC pattern until early 2019, but I now recommend to use these instead.

bloc_provider will now be minimally maintained.

Usage

1. Define some BLoC like this:
class CounterBloc implements Bloc {
  final _countController = BehaviorSubject<int>.seeded(0);
  final _incrementController = PublishSubject<void>();

  CounterBloc() {
    _incrementController
        .scan<int>((sum, _v, _i) => sum + 1, 0)
        .pipe(_countController);
  }

  ValueStream<int> get count => _countController;
  Sink<void> get increment => _incrementController.sink;

  @override
  void dispose() async {
    await _incrementController.close();
    await _countController.close();
  }
}
2. Provide the bloc by using BlocProvider and access the bloc at subtree:
void main() => runApp(
      // Create and provide the bloc.
      BlocProvider<CounterBloc>(
        creator: (_context, _bag) => CounterBloc(),
        child: App(),
      ),
    );

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // Access the bloc with O(1) computation complexity.
    final bloc = BlocProvider.of<CounterBloc>(context);
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: StreamBuilder<int>(
            stream: bloc.count,
            initialData: bloc.count.value,
            builder: (context, snap) => Text(
                  'count: ${snap.data}',
                  style: Theme.of(context).textTheme.title,
                ),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          child: const Icon(Icons.add),
          onPressed: () => bloc.increment.add(null),
        ),
      ),
    );
  }
}
  • Computational complexity of of method, which is used for accessing the bloc is O(1).
  • Provided bloc will be disposed when the inner state is disposed 👍

Examples

Technical explanation

Features and bugs

Please file feature requests and bugs at the issue tracker.

所属分类

源码仓库

使用此插件的项目