搜尋套件、儲存庫與 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.

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。