実現したいこと
以下のようにFireStoreに登録をしたいです。
-attendaces -uid -uid -name -createdAt -attendance
以下Modelファイルです
Dart
1import 'package:cloud_firestore/cloud_firestore.dart'; 2import 'package:firebase_auth/firebase_auth.dart'; 3import 'package:flutter/material.dart'; 4 5class QRModel extends ChangeNotifier { 6 // final uidController = TextEditingController(); 7 // final nameController = TextEditingController(); 8 // final attendanceController = TextEditingController(); 9 10 String? uid; 11 String? name; 12 String? attendance; 13 14 bool isLoading = false; 15 16 void startLoading() { 17 isLoading = true; 18 notifyListeners(); 19 } 20 21 void endLoading() { 22 isLoading = false; 23 notifyListeners(); 24 } 25 26 void setUid(String uid) { 27 this.uid = uid; 28 notifyListeners(); 29 } 30 31 void setName(String name) { 32 this.name = name; 33 notifyListeners(); 34 } 35 36 void setAttendance(String attendance) { 37 this.attendance = attendance; 38 notifyListeners(); 39 } 40 41 Future signUp() async { 42 // this.uid = uidController.text; 43 //this.name = nameController.text; 44 //this.attendance = attendanceController.text; 45 46 var user = FirebaseAuth.instance.currentUser; 47 48 if (user != null) { 49 print(uid); 50 // firestoreに追加 51 final doc = FirebaseFirestore.instance.collection('attendaces').doc(uid); 52 await doc.set({'createdAt': Timestamp.now()}, SetOptions(merge: true)); 53 await doc.set({ 54 'uid': uid, 55 'name': name, 56 // 'timeStamp': timeStamp, 57 'attendance': attendance, 58 }); 59 } 60 } 61} 62
変更を伝えるファイル
このファイルでChangeNotifierProviderでmodelに変数の値を渡したいのですが
TextFieldのように動的にTextを変えるのではなく、currentUserの定数?を
どこかでセットしたいです。
TextFieldを一応試しましたが上手くいかなかったので遷移なしで値を渡せる方法はありますでしょうか?
Dart
1import 'package:attendanc_management_app/scan_qr_code/qr_model.dart'; 2import 'package:cloud_firestore/cloud_firestore.dart'; 3import 'package:firebase_auth/firebase_auth.dart'; 4import 'package:flutter/material.dart'; 5import 'package:provider/provider.dart'; 6import '../mypage/my_model.dart'; 7 8class AttendaveRegister extends StatelessWidget { 9 10 String? uid; 11 String? email; 12 String? name; 13 String? attendance; 14 final currentUser = FirebaseAuth.instance.currentUser; 15 16 17 Widget build(BuildContext context) { 18 this.uid = currentUser!.uid; 19 this.email = currentUser!.email; 20 this.name = currentUser!.displayName; 21 this.timeStamp = DateTime.now().millisecondsSinceEpoch; 22 var dateNow = 23 DateFormat('MM/dd kk:mm').format(Timestamp.now().toDate()).toString(); 24 this.attendance = "出席"; 25 26 return ChangeNotifierProvider<QRModel>( 27 create: (_) => QRModel(), 28 child: Scaffold( 29 appBar: AppBar( 30 title: Text("出席登録"), 31 ), 32 body: Center( 33 child: Container( 34 height: deviceHeight * 0.5, 35 width: double.infinity, 36 child: Consumer<QRModel>(builder: (context, model, child) { 37 return Stack(children: [ 38 Padding( 39 padding: const EdgeInsets.only(top: 50.0), 40 child: Column( 41 children: [ 42 Row( 43 mainAxisAlignment: MainAxisAlignment.center, 44 children: [ 45 Container( 46 height: deviceHeight * 0.25, 47 width: deviceWidth * 0.75, 48 child: Column(children: [ 49 // FireStore保存用InvisibletextField 50 Visibility( 51 visible: false, 52 child: TextField( 53 enabled: false, 54 controller: model.uidController, 55 onChanged: (text) { 56 model.setUid(uid!); 57 }, 58 ), 59 ), 60 Visibility( 61 visible: false, 62 child: TextField( 63 enabled: false, 64 controller: model.nameController, 65 onChanged: (text) { 66 model.setName(name!); 67 }, 68 ), 69 ), 70 71 Visibility( 72 visible: false, 73 child: TextField( 74 enabled: false, 75 controller: model.attendanceController, 76 onChanged: (text) { 77 model.setAttendance(attendance!); 78 }, 79 ), 80 ), 81 ]), 82 ), 83 ], 84 ), 85 Text("上記内容で出席しますか?"), 86 ElevatedButton( 87 onPressed: () async { 88 model.startLoading(); 89 90 // 追加の処理 91 try { 92 await model.signUp(); 93 Navigator.of(context).pop(); 94 } catch (e) { 95 final snackBar = SnackBar( 96 backgroundColor: Colors.red, 97 content: Text(e.toString()), 98 ); 99 ScaffoldMessenger.of(context) 100 .showSnackBar(snackBar); 101 } finally { 102 model.endLoading(); 103 } 104 }, 105 child: Text('出席する'), 106 ), 107 ], 108 ), 109 ), 110 ]); 111 }), 112 ), 113 ), 114 ), 115 ); 116 // throw UnimplementedError(); 117 } 118} 119
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。