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

所屬分類

原始碼儲存庫

使用此套件的專案