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

插件详情v2.1.1

generic_bloc_provider

适用于您 Flutter 应用的通用 BloC 提供者。此包可帮助您避免编写 BloC 提供者的样板代码。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
561
喜欢数
22
Pub 分数
160

安装插件

flutter pub add generic_bloc_provider

插件介绍

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

一个 Flutter 包,通过提供通用的 BloC 提供者来简化状态管理,减少设置和管理 BloC 实例时通常所需的样板代码。它使开发者能够轻松地在小部件树中注入和访问 BloC 类,具有类型安全性和最少的设置。适用于使用 BloC 模式的应用,它简化了依赖注入并增强了代码可维护性。

适合场景

  • 在大型应用中简化 BloC 实例化
  • 减少重复的提供者样板代码
  • 高效管理多个 BloC 实例
  • 启用对 BloCs 的类型安全访问
  • 支持关注点的清晰分离

主要特点

  • BloC 类的泛型类型支持
  • 最小化样板代码
  • 类型安全的提供者访问
  • 与现有 BloC 模式轻松集成
  • 自动生命周期管理

适合对象

  • 使用 BloC 模式的开发者
  • 旨在减少重复代码的项目
  • 具有多个状态管理器的应用程序
  • 优先考虑可维护的状态逻辑的团队
  • Flutter 项目在多个平台

使用建议

  • 需要理解 BloC 模式
  • 不适合简单的状态需求
  • 可能增加小型应用程序的复杂性
  • 最好与其他状态管理工具一起使用
  • 与手动提供者相比,自定义选项有限

原始 README

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

generic_bloc_provider

Build Status License: MIT Pub

A generic BloC Provider for your Flutter apps.

Getting Started

All your BloCs must extend the Bloc abstract class. This means that all of them should have a dispose method that will be executed whenever the life of the BloC comes to an end.

In this method, you should take care of closing all the sinks and resources.

Example of how to use it:

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      bloc: AppBloc(),
      child: MaterialApp(
        title: 'Yo Sleep',
        home: MainPage(),
        initialRoute: 'main',
        routes: {
          'main': (context) => MainPage(),
        },
      ),
    );
  }
}

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final appBloc = BlocProvider.of<AppBloc>(context);
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Header(),
            RecordList(appBloc),
          ],
        ),
      ),
    );
  }
}

Attaching your context to the InheritedWidget

The BlocProvider class depends on InheritedWidget. Whenever you want to get your BloC, you can decide wether to attach the context of your widget to the InheritedWidget or not.

In order to control this behavior, the static method of has an optional boolean argument (which is true by default) which determines wether your context will be attached or not.

Basically, if you don't provide it or you just set it to true, dependOnInheritedWidgetOfExactType will be used. If you set it to false then getElementForInheritedWidgetOfExactType will be used instead.

Customizing the update policy

If you want to change the way updateShouldNotify behaves you have the option to provide a custom anonymous function to the BlocProvider constructor.

You will notice that there's an argument called updateShouldNotifyOverride which accepts a function receiving a BloC and the internal InheritedWidget:

bool Function(T bloc, _BlocProvider oldWidget);

This is the default implementation of the updateShouldNotify method:

@override
  bool updateShouldNotify(_BlocProvider oldWidget) =>
      updateShouldNotifyOverride != null
          ? updateShouldNotifyOverride!(bloc, oldWidget)
          : oldWidget.bloc != bloc;

所属分类

源码仓库

使用此插件的项目