Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Flutter Riverpodのすゝめ

hammer0802
August 21, 2020

Flutter Riverpodのすゝめ

2020年8月21日 Yumemi.apk #1

hammer0802

August 21, 2020
Tweet

More Decks by hammer0802

Other Decks in Technology

Transcript

  1. 2020/08/21 ͸Μ·ʔ @ Flutter Riverpodͷ͢ʍΊ @Yumemi.apk #1 Flutter and the

    related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC.
  2. ͸Μ·ʔ • AndroidΤϯδχΞ • 2019೥8݄גࣜձࣾΏΊΈத్ೖࣾ • ݩϝΠϯϑϨʔϜΤϯδχΞ • 6݄ʹ௕ঁ஀ੜ •

    Twitter : @watashi_desu_82 • Qiita : @hammer0802 ࣗݾ঺հ ↑ࢲͷ෼਎
 ϋϯϚϩΠυ͘Μ 2
  3. Flutterɹɹͱ͸ʁ • ϚϧνϓϥοτϑΥʔϜϑϨʔϜϫʔΫʢAndroid / iOS / Web / Desktopʣ •

    Google੡ • એݴతUIɹ͜͏͍͏ͷˠ • ݴޠ͸Dart • JITʢJust In TimeʣͱAOTʢAhead Of Timeʣ྆ํͷίϯύΠϥΛαϙʔτ
 →Hot Reload͕ศར Flutter and the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC. 6
  4. એݴతUIͰ͋Γ͕ͪͳঢ়ଶ؅ཧ໰୊ Parent Level 1 Level 2 Level 3 Data Prop

    Drilling ͜͜Ͱࢀর͍ͨ͠ όέπϦϨʔͷΑ͏ʹ σʔλΛड͚౉ͯ͠͠ ͠·͏ʢLevel 1, 2Ͱ ඞཁͳ͍ͷʹʣ 7
  5. Providerͱ͸ʁ • ঢ়ଶ؅ཧ͓ΑͼDIͷ࢓૊ΈΛఏڙ͢ΔϥΠϒϥϦ • Widget΁DI͢Δ͜ͱ͕Ͱ͖ɺҾ਺Ͱ஋Λ౉͢ඞཁͳ͠
 →Prop Drillingճආ • ChangeNotifier΍StateNotifierΛ࢖ͬͯWidgetʹ஋Λ௨஌͠ɺUIߋ৽
 →ঢ়ଶͱϩδοΫΛWidget͔Β෼཭

    • ஋Λࢀর͍ͯ͠ΔWidget͚͕ͩϦϏϧυ
 →ύϑΥʔϚϯε޲্ Foo( child: Consumer<A>( builder: (_, a, child) { return Bar(a: a, child: child); }, child: Baz(), ), ) a͕ߋ৽͞ΕͨΒ
 Bar͚ͩϦϏϧυ 11
  6. Flutter͡Όͳͯ͘΋࢖͑Δ • BuildContextʹґଘ
 →FlutterͰ͔͠࢖͑ͳ͍ • ࠶ߏஙͨ͜͠ͱͰBuildContext͔Β୤ґଘ
 →Pure DartͰ΋࢖͑ΔΑ͏ʹͳͬͨʂ Flutter and

    the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC.
 Dart and the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC. 15
  7. άϩʔόϧม਺ͰProviderΛએݴͰ͖Δ main.dart void main() { runApp( StateNotifierProvider<HogeNotifer, String>( create: (_)

    => HogeNotifer(), child: MyApp(), ); } • ΞϓϦͷϧʔτͰએݴ
 →ProviderΛฤू͢Δͨͼʹ main.dartΛߦ͖དྷ… • ProviderScope഑ԼͳΒͲ͜Ͱ΋એݴՄೳ
 →WidgetͱProviderΛ1ϑΝΠϧʹू໿Ͱ͖Δ main.dart void main() { runApp( ProviderScope( child: MyApp(), ), ); } app.dart final hogeProvider = StateNotifierProvider((ref) => HogeNotifier() ); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Consumer( (context, read) { final hoge = read(hogeProvider); return Text(hoge); }, ), ), ); } } 16
  8. ಉ͡ܕͷProviderΛෳ਺࣋ͯΔ • ʮWidget πϦʔΛ͔͞ͷ΅ͬͯ࠷دΓ ͷ Provider<T> Λऔಘ͠ɺͦͷ஋Λฦ ͢ʯ࢓༷ͷͨΊɺಉ͡ܕͷProviderΛ ෳ਺࣋ͯͳ͍ MultiProvider(

    providers: [ Provider<String>.value(value: ‘Mona’), Provider<String>.value(value: ‘Lisa’), ], child: HogeWidget(context), ) Widget HogeWidget(BuildContext context) { final firstName = Provider.of<String>(context); final lastName = Provider.of<String>(context); print(firstName); // Lisa͕දࣔ͞ΕΔ print(lastName); // Lisa͕දࣔ͞ΕΔ } • ಉ͡ܕͰ΋ෳ਺ͷProviderΛಠཱͯ͠ ࣋ͭ͜ͱ͕Ͱ͖Δ final firstNameProvider = Provider<String>((ref) => ‘Mona'); final lastNameProvider = Provider<String>((ref) => ‘Lisa’); : : Consumer( (context, read) { final firstName = read(firstNameProvider); final lastName = read(lastNameProvider); print(firstName); // Mona͕දࣔ͞ΕΔ print(lastName); // Lisa͕දࣔ͞ΕΔ } 17
  9. ঢ়ଶͷܭࢉ݁ՌΛProviderͱͯ࣋ͯ͠Δ • Provider͔ΒผͷProviderͷঢ়ଶΛಡΈࠐΈɺͦΕΛܭࢉͨ݁͠ՌΛProvider ͱͯ࣋ͭ͜͠ͱ͕Ͱ͖Δ • watch͍ͯ͠ΔProviderͷঢ়ଶ͕มߋ͞Εͯ΋ɺܭࢉ݁Ռͷঢ়ଶ͕มΘΒͳ͍ ৔߹͸ࢀর͍ͯ͠ΔWidget͕ϦϏϧυ͞Εͳ͍ • ϑΟϧλʔ΍ιʔτͳͲͷػೳͰ࢖͏ͱύϑΥʔϚϯε࠷దԽ͕Ͱ͖Δ final

    firstNameProvider = StateProvider<String>((ref) => ‘Mona'); final lastNameProvider = StateProvider<String>((ref) => ‘Lisa’); final initialsProvider = Provider((ref) { final firstName = ref.watch(firstNameProvider).state; final lastName = ref.watch(lastNameProvider).state; return '${firstName[0]}${lastName[0]}'; }); home: Scaffold( body: Center( child: Consumer( (context, read) { final initials = read(initialsProvider); return Text(initials); // ML͕දࣔ͞ΕΔ }, ), ), 18
  10. ֎෦ύϥϝʔλ͔ΒProviderΛ࡞੒Ͱ͖Δ • Ҿ਺ʹ஋Λ෇༩ͯ͠providerʹ౉͢͜ͱ͕Ͱ͖Δ • ࢖༻͢Δʹ͸ .family Λ࢖͏ // userIdΛ౉͠ɺUserRepository.fetch()Λ࢖ͬͯඇಉظͰϢʔβʔ৘ใΛऔಘ͢ΔͱԾఆ final

    userProvider = FutureProvider.family<User, int>((ref, userId) async { final userRepository = ref.read(userRepositoryProvider); return await userRepository.fetch(userId); }); 19
  11. ෆཁͳProviderΛࣗಈͰഁغͰ͖Δ • લϖʔδͷίʔυͩͱɺUserΛऔಘ͢ΔͨͼUserͷProvider͕૿͍͑ͯͬͯ͠ ·͏ • ෆཁʹͳͬͨΒࣗಈతʹഁغͰ͖ΔΦϓγϣϯ͕͋Δʢ.autoDisposeʣ • .familyͱ.autoDispose͸ಉ࣌ʹ࢖͑Δ final userProvider

    = FutureProvider.family.autoDispose<User, int>((ref, userId) async { final userRepository = ref.read(userRepositoryProvider); return await userRepository.fetch(userId); }); 20