背景
Riverpod を勉強中です。
サンプルとして
- FAB をタップしたら値が更新される
- ロングタップすると画面遷移される
というものを作ろうとしています。
class User
や currentUser
という言葉が並ぶのはアプリケーションで画面をまたいで共有したいデータという意味で使っています
問題
class Home
においては、Fabのタップごとに値が更新されますが、 class Next
では値が更新されません。
それぞれ画面のFabでタップ時に呼んでいるメソッドは
class Home
:値を全て更新するCurrentUser#setUser
class Next
:値を一部更新するCurrentUser#setUserId
となっており、具体的にはこの CurrentUser#setUserId
で値が更新されません
どなたか解決策がわかるかたいませんか?
ソースコード
main.dart
1import 'dart:math'; 2 3import 'package:flutter/material.dart'; 4import 'package:hooks_riverpod/hooks_riverpod.dart'; 5import 'package:routemaster/routemaster.dart'; 6 7class User { 8 final int id; 9 final String name; 10 11 User({required this.id, required this.name}); 12} 13 14 15class CurrentUser extends StateNotifier<User?> { 16 CurrentUser(User? state) : super(state); 17 18 void setUser(User? user) { 19 state = user; 20 } 21 22 void setUserId(int n) { 23 final user = User(id: state?.id ?? 0, name: state?.name ?? ""); 24 state = user; 25 } 26} 27 28final currentUserProvider = 29 StateNotifierProvider<CurrentUser, User?>((_) => CurrentUser(null)); 30 31void main() { 32 runApp( 33 const ProviderScope(child: MyApp()), 34 ); 35} 36 37class MyApp extends StatelessWidget { 38 const MyApp({Key? key}) : super(key: key); 39 40 @override 41 Widget build(BuildContext context) { 42 return MaterialApp.router( 43 routeInformationParser: const RoutemasterParser(), 44 routerDelegate: RoutemasterDelegate( 45 routesBuilder: (_) => RouteMap(routes: { 46 "/": (_) => const MaterialPage(child: Home()), 47 "/next": (_) => const MaterialPage(child: Next()), 48 })), 49 ); 50 } 51} 52 53class Home extends HookConsumerWidget { 54 const Home({Key? key}) : super(key: key); 55 56 @override 57 Widget build(BuildContext context, WidgetRef ref) { 58 final currentUser = ref.watch(currentUserProvider); 59 60 return Scaffold( 61 appBar: AppBar(title: const Text('Counter example')), 62 body: Center( 63 child: Text((currentUser?.id ?? 0).toString()), 64 ), 65 floatingActionButton: InkWell( 66 onLongPress: () => Routemaster.of(context).push('/next'), 67 child: FloatingActionButton( 68 onPressed: () => ref 69 .read(currentUserProvider.notifier) 70 .setUser(User(id: Random().nextInt(10000), name: 'potter')), 71 child: const Icon(Icons.add), 72 ), 73 ), 74 ); 75 } 76} 77 78class Next extends HookConsumerWidget { 79 const Next({Key? key}) : super(key: key); 80 81 @override 82 Widget build(BuildContext context, WidgetRef ref) { 83 return Scaffold( 84 appBar: AppBar( 85 title: const Text('next'), 86 ), 87 body: Center( 88 child: Text((ref.watch(currentUserProvider)?.id ?? 0).toString()), 89 ), 90 floatingActionButton: InkWell( 91 onLongPress: () => Routemaster.of(context).push('/'), 92 child: FloatingActionButton( 93 onPressed: () { 94 ref.read(currentUserProvider.notifier).setUserId(Random().nextInt(10000)); 95 }, 96 child: const Icon(Icons.add), 97 ), 98 )); 99 } 100}
pubspec.yaml
1name: riverpod_sample_code 2description: A new Flutter project. 3 4publish_to: 'none' # Remove this line if you wish to publish to pub.dev 5 6version: 1.0.0+1 7 8environment: 9 sdk: ">=2.15.1 <3.0.0" 10 11dependencies: 12 flutter: 13 sdk: flutter 14 15 cupertino_icons: ^1.0.2 16 flutter_hooks: ^0.18.0 17 hooks_riverpod: ^1.0.3 18 routemaster: ^0.9.5 19 20dev_dependencies: 21 flutter_test: 22 sdk: flutter 23 24 flutter_lints: ^1.0.0 25 26flutter: 27 uses-material-design: true
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/04/03 08:25