パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細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;

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト