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

所屬分類

原始碼儲存庫

使用此套件的專案