前提
Flutterでユーザー編集の更新ボタンを作っています。
DropdwonButtonのウィジェットをタップすると、リストは出ますが
タップ後に文字列が反映されません。
実現したいこと
ボタンにリストの文字列を反映させる。
発生している問題・エラーメッセージ
該当のソースコード
page
1 2class Page extends StatelessWidget { 3 Page({Key? key, required this.user}); 4 DocumentSnapshot user; 5 String value = "user"; 6 7 @override 8 Widget build(BuildContext context) { 9 return ChangeNotifierProvider<EditAdminUserModel>( 10 create: (_) => EditAdminUserModel(user), 11 child: Scaffold( 12 appBar: AppBar(), 13 body: Center( 14 child: Consumer<EditAdminUserModel>( 15 builder: (context, model, child) { 16 return Padding( 17 padding: const EdgeInsets.all(30.0), 18 child: Column( 19 mainAxisAlignment: MainAxisAlignment.center, 20 children: [ 21 DropdownButton( 22 items: 23 model.items.map<DropdownMenuItem<String>>((value) { 24 return DropdownMenuItem<String>( 25 value: model.isSelectedItem, 26 child: Text(value!), 27 ); 28 }).toList(), 29 onChanged: (String? value) { 30 model.DropDownValue(value); 31 }), 32 ElevatedButton( 33 onPressed: () async { 34 try { 35 await model.update(); 36 Navigator.of(context).pop(true); 37 } catch (e) { 38 final snackBar = 39 SnackBar(content: Text(e.toString())); 40 ScaffoldMessenger.of(context) 41 .showSnackBar(snackBar); 42 } 43 }, 44 child: const Text("更新する")) 45 ], 46 ), 47 ); 48 }, 49 ), 50 ), 51 ), 52 ); 53 } 54}
provider
1 2class EditAdminUserModel extends ChangeNotifier { 3 EditAdminUserModel(this.user) { 4 5 roleContorller.text = user["role"]!; 6 7 } 8 9 String? role; 10 String? uid; 11 12 13 final roleContorller = TextEditingController(); 14 15 16 List<String?> items = ["admin", "user"]; 17 18 DocumentSnapshot user; 19 String? isSelectedItem = "user"; 20 21 void setName(String name) { 22 this.name = name; 23 notifyListeners(); 24 } 25 26 void setimagePath(String imagePath) { 27 this.imagePath = imagePath; 28 notifyListeners(); 29 } 30 31 void setstatus(String status) { 32 this.status = status; 33 notifyListeners(); 34 } 35 36 Future update() async { 37 38 39 if (role == null || role == "") { 40 throw "役割が入力されていません"; 41 } 42 43 44 this.role = roleContorller.text; 45 46 this.uid = user["uid"]; 47 48 await FirebaseFirestore.instance.collection("users").doc(uid).update( 49 {"role": role, }); 50 } 51 52 dynamic DropDownValue(String? value) { 53 isSelectedItem = value; 54 55 notifyListeners(); 56 } 57} 58 59
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/09/12 00:38