LoginSignup
2
0

More than 3 years have passed since last update.

【Flutter】Providerのオプションと意味を総まとめ!

Last updated at Posted at 2020-04-18

Providerパッケージにはさまざまな種類のProviderがProviderの種類やコンストラクタの種類によって渡せる引数(オプション)が異なり混乱のもととなるので、コンストラクタが取りうる引数の種類一覧と、各コンストラクタごとに使える引数を以下にまとめます。

コンストラクタが取りうる引数一覧

Tは保持するオブジェクトの型。
(Provider<T> ←これ)

  • Key key
    • ふつうのwidgetのkeyと同じ
  • Widget child
  • bool lazy
  • UpdateShouldNotify<T> updateShouldNotify
    • Providerでは、InheritedWidgetでは継承先でoverrideしていたupdateShouldNotifyを引数として渡して定義できる。
    • UpdateShouldNotifyの定義は、typedef UpdateShouldNotify<T> = bool Function(T previous, T current);

デフォルトコンストラクタ専用

  • Crate<T> create
    • 保持するオブジェクトを生成する(以下valueと呼ぶ)
    • Createの定義は、typedef Create<T> = T Function(BuildContext context);
  • Dispose<T> dispose
    • Providerがunmountされるときに呼ばれる(State.disposeとほぼ同じ)
    • Disposeの定義は、typedef Dispose<T> = void Function(BuildContext context, T value);

value()コンストラクタ専用

  • T value
    • Providerに渡されるオブジェクト(以下valueと呼ぶ)

デフォルトコンストラクタは、createで生成したvalueに対して最期までdisposeで面倒を見るという感じ。
value()コンストラクタは、よそから来たvalueをちょっと使わせてもらうだけで一生の面倒は見ない。

FutureProvider/StreamProvider専用

  • T initialData
    • StreamProviderではstreamに値がまだ流れてきていないときの初期値
    • FutureProviderではFutureが完了する前に使う値
  • ErrorBuilder<T> catchError
    • エラーが起こったときにもvalidなvalueを生成するための引数
      • ErrorBuilderの定義はtypedef ErrorBuilder<T> = T Function(BuildContext context, Object error);

Proxy系Provider専用

(Proxy系ProviderはProxyProvider以外にもいろいろな亜種がある。今回は紹介しない)

  • T update
    • Proxyが依存する値からvalueを生成する。
    • typedefで定義されるが、Providerの種類によって形が変わる。

Providerごとの渡せる引数まとめ

Provider<T>

デフォルトコンストラクタ

  • Key key
  • Crate<T> create(必須)
  • Dispose<T> dispose
  • bool lazy
  • Widget child

value()コンストラクタ

  • Key key
  • T value(必須)
  • UpdateShouldNotify<T> updateShouldNotify
  • Widget child

ListenableProvider<T extends Listenable>

デフォルトコンストラクタ

  • Key key
  • Crate<T> create(必須)
  • Dispose<T> dispose
  • bool lazy
  • Widget child

value()コンストラクタ

  • Key key
  • T value(必須)
  • UpdateShouldNotify<T> updateShouldNotify
  • Widget child

いずれの引数もProviderと同じ

ChangeNotifierProvider<T extends ChangeNotifier>

デフォルトコンストラクタ

  • Key key
  • Create create(必須)
  • bool lazy
  • Widget child

disposeは渡すことができない。代わりに、自動でChangeNotifier.dispose()を呼んでくれる。

value()コンストラクタ

  • Key key
  • T value(必須)
  • Widget child

いずれのコンストラクタにもupdateShouldNotifyがない。
値の変更の通知はChangeNotifier.notifyListeners()にまかせているためだと思われる。

ValueListenableProvider<T>

ValueListenableは抽象クラスで、implementはValueNotifierクラスAnimationクラス
AnimationもProviderに渡せる!!

デフォルトコンストラクタ

  • Key key
  • Create <ValueNotifier<T>> create(必須)
  • UpdateShouldNotify<T> updateShouldNotify
  • bool lazy
  • Widget child

value()コンストラクタ

  • Key key
  • ValueListenable<T> value(必須)
  • UpdateShouldNotify<T> updateShouldNotify
  • Widget child

FutureProvider<T>

デフォルトコンストラクタ

  • Key key
  • Create<Future<T>> create(必須)
  • T initialData
  • ErrorBuilder<T> catchError
  • UpdateShouldNotify<T> updateShouldNotify
  • bool lazy
  • Widget child

value()コンストラクタ

  • Key key
  • Future<T> value(必須)
  • ErrorBuilder<T> catchError
  • UpdateShouldNotify<T> updateShouldNotify
  • bool lazy
  • Widget child

StreamProvider<T>

デフォルトコンストラクタ

  • Key key
  • Create<Stream<T>> create(必須)
  • T initialData
  • ErrorBuilder<T> catchError
  • UpdateShouldNotify<T> updateShouldNotify
  • bool lazy
  • Widget child

value()コンストラクタ

  • Key key
  • Stream<T> value(必須)
  • ErrorBuilder<T> catchError
  • UpdateShouldNotify<T> updateShouldNotify
  • bool lazy
  • Widget child

ProxyProvider

ここではTは依存している値の型。Rは変換後の値の型。

デフォルトコンストラクタ

  • Key key
  • Create create
  • ProxyProviderBuilder update(必須)
    • ProxyProviderBuilderの定義は、typedef ProxyProviderBuilder<T, R> = R Function( BuildContext context, T value, R previous, );
  • UpdateShouldNotify updateShouldNotify
  • Dispose dispose
  • bool lazy
  • Widget child

value()コンストラクタはない。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0