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

插件详情v1.0.8

dash

Dash 库负责提供一种服务定位器模式,该模式关注于 bloc 的提供和释放。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
182
喜欢数
10
Pub 分数
160

安装插件

flutter pub add dash

插件介绍

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

Dash 是一个轻量级的 Flutter 服务定位包,通过管理 bloc 的生命周期来简化依赖注入,确保正确实例化和释放。它提供了一种简洁的方式来注册和获取依赖项,特别适用于使用 Bloc 模式进行状态管理。该库支持所有主要平台,并可无缝集成到现有的 Flutter 项目中。其对生命周期的重视有助于防止内存泄漏并提升应用性能。

适合场景

  • 在Flutter应用中管理Bloc生命周期
  • 简化依赖注入
  • 替换手动创建和释放 Bloc
  • 与状态管理模式集成
  • 简化测试设置
  • 启用模块化应用架构

主要特点

  • 自动 bloc 清理
  • 简单注册和检索
  • 平台无关设计
  • 内置生命周期管理
  • 最小化样板代码
  • 支持嵌套依赖项

适合对象

  • 使用 Bloc 模式的 Flutter 开发人员
  • 需要结构化依赖管理的应用程序
  • 寻求轻量级依赖注入解决方案的团队
  • 致力于干净状态管理的项目
  • 想要自动资源清理的开发者

使用建议

  • 仅限于Bloc相关的使用场景
  • 需要理解服务定位器模式
  • 不适合复杂的依赖注入需求
  • 可能需要为高级场景进行额外设置
  • 功能不如完整的DI框架丰富

原始 README

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

Dash

Logo

Dash is a simple but efficient library that helps provide BloCs without headaches.

IMPORTANT: Dart2 is required to use this package.

Installation

  • Add dash to your pubspec.yaml under the dependencies section:
dependencies:
  dash: ^latest_version
  • Add build_runner and dash_generator to your pubspec.yaml under the dev_dependencies section:
dev_dependencies:
  build_runner: ^2.0.3
  dash_generator: ^latest_version

Usage

1 - Create a provider class that will gather BlocProvider annotation:

IMPORTANT: Do not forget to add: part 'provider.g.dart';.

import 'package:dash/dash.dart';

part 'provider.g.dart';

@BlocProvider.register(MySampleBloc)
@BlocProvider.register(MyOtherBloc)
abstract class Provider {}

2 - Extend BLoCs from Bloc. It will allow you to override dispose method.

IMPORTANT: All blocs must have an instance() function that returns the instance of the bloc as below.

class MySampleBloc extends Bloc {

  @override
  dispose() {
    //close all streams
  }

  static Bloc instance() => MySampleBloc();
  
}

class MyOtherBloc extends Bloc {

  @override
  dispose() {
    //close all streams
  }

  static Bloc instance() => MyOtherBloc();
}

3 - Run build_runner, it will generate the provider.g.dart class that you added as part on your provider class.

On project directory run in terminal:

pub run build_runner build

For flutter:

flutter packages pub run build_runner build

Note: If you encounter a conflict error, please add the --delete-conflicting-outputs argument to your command:

flutter packages pub run build_runner build --delete-conflicting-outputs

4 - Finally you can get your BLoC instance from anywhere, for example:

  @override
  Widget build(BuildContext context) {
    final _bloc = $Provider.of<MyOtherBloc>();

    return Container();
  }

  @override
  void dispose() {
    $Provider.dispose<MyOtherBloc>();
    super.dispose();
  }

Changelog

Please see the Changelog page to know what's recently changed.

所属分类

源码仓库

使用此插件的项目

暂无相关数据。