LoginSignup
2
1

More than 3 years have passed since last update.

【Flutter】nestedパッケージと理解するMutliProvider

Last updated at Posted at 2020-04-17

Providerの内部では、nestedというパッケージが使われています。
nestedパッケージを理解して、MultiProviderが実際にはproviderのネストを簡単に記述できるようにしているだけであることを確認します。
MultiProviderでは実質的には表記順にもとづいて親子関係が作られているので、順番が問題になることがあります。

nestedパッケージ

https://pub.dev/packages/nested の通り、

 MyWidget(
   child: AnotherWidget(
     child: Again(
       child: AndAgain(
         child: Leaf(),
       )
     )
   )
 )

を、MyWidget~AndAgainをすべてSingleChildStatelessWidgetもしくはSingleChildStatefulWidgetを継承させて書き換えることで以下のように書き換えられる。

 Nested(
   children: [
     MyWidget(),
     AnotherWidget(),
     Again(),
     AndAgain(),
   ],
   child: Leaf(),
 )

MyWidgetの書き換え例は以下。
SingleChildStatelessWidgetは、buildのかわりにbuildWithChildをoverrideさせる。(buildをoverrideしてはいけない)

 class MyWidget extends StatelessWidget {
   MyWidget({Key key, this.child}): super(key: key);
 
   final Widget child;
 
   @override
   Widget build(BuildContext context) {
     return SomethingWidget(child: child);
   }
 }
 class MyWidget extends SingleChildStatelessWidget {
   MyWidget({Key key, Widget child}): super(key: key, child: child);
 
   @override
   Widget buildWithChild(BuildContext context, Widget child) {
     return SomethingWidget(child: child);
   }
 }

MultiProvider

Providerの内部では、すべてのProviderの祖先であるInheritedWidgetSingleChildStatelessWidgetを継承し、MultiProviderNestedを継承している。
そうすることで、

 Provider<Something>(
   create: (_) => Something(),
   child: Provider<SomethingElse>(
     create: (_) => SomethingElse(),
     child: Provider<AnotherThing>(
       create: (_) => AnotherThing(),
       child: someWidget,
     ),
   ),
 )

を、

 MultiProvider(
   providers: [
     Provider<Something>(create: (_) => Something()),
     Provider<SomethingElse>(create: (_) => SomethingElse()),
     Provider<AnotherThing>(create: (_) => AnotherThing()),
   ],
   child: someWidget,
 )

のように書けるようにしている。(最初の例と同じように)

2
1
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
1