質問するログイン新規登録

質問編集履歴

2

プログラムの修正

2021/06/19 14:09

投稿

kimidoro
kimidoro

スコア11

title CHANGED
File without changes
body CHANGED
@@ -6,37 +6,44 @@
6
6
  ご教授お願いいたします。
7
7
 
8
8
  ---
9
- ```ここに言語を入力
9
+ ```dart
10
10
 
11
11
  class TimerModel extends ChangeNotifier{
12
12
 
13
13
  List<Time> timeList = [];
14
14
  String Uid = '';
15
15
 
16
- Future getUid ()async{
16
+ Future getUid(){
17
- final snapshot = await FirebaseAuth.instance.currentUser;
17
+ final snapshot = FirebaseAuth.instance.currentUser;
18
18
  this.Uid = snapshot.uid;
19
19
  }
20
20
 
21
21
  Future gettimerList() async {
22
22
 
23
23
  final snapshot =
24
- await FirebaseFirestore.instance
24
+ await FirebaseFirestore
25
+ .instance
25
- .collection('users').doc(Uid)
26
+ .collection('users')
27
+ .doc(Uid)
26
- .collection('timer').get();
28
+ .collection('timer')
29
+ .get();
27
30
  final docs = snapshot.docs;
28
31
  final timeList = docs.map((doc) => Time(doc)).toList();
29
32
  this.timeList = timeList;
30
33
 
31
34
  notifyListeners();
35
+
32
36
  }
33
37
 
34
38
  Future getTeaListRealtime() async {
35
39
 
36
40
  final snapshots =
37
- await FirebaseFirestore.instance
41
+ await FirebaseFirestore
42
+ .instance
38
- .collection('users').doc(Uid)
43
+ .collection('users')
44
+ .doc(Uid)
39
- .collection('timer').snapshots();
45
+ .collection('timer')
46
+ .snapshots();
40
47
  snapshots.listen((snapshot){
41
48
  final docs = snapshot.docs;
42
49
  final timeList = docs.map((doc) => Time(doc)).toList();
@@ -45,4 +52,59 @@
45
52
  });
46
53
  }
47
54
  }
48
- ```
55
+ ```
56
+ ```dart
57
+ class TimerList extends StatelessWidget {
58
+ @override
59
+ Widget build(BuildContext context) {
60
+
61
+ return Consumer<TimerModel>(builder: (context, model, child) {
62
+ model.getUid();
63
+ return Scaffold(
64
+ appBar: AppBar(
65
+ title: Text('タイマーリスト'),
66
+ centerTitle: true,
67
+ actions: [
68
+ InkWell(
69
+ onTap: ()async{
70
+ print(model.Uid); ※ここでuidを出力できました。
71
+ },
72
+ child: Icon(Icons.favorite_border,
73
+ ),
74
+ )
75
+ ],
76
+ ),
77
+ body:Consumer<TimerModel>(builder: (context, model, child) {
78
+ final timerList = model.timeList;
79
+ return ListView(
80
+ children: timerList
81
+ .map(
82
+ (timelist) => Card(
83
+ child: Center(
84
+ child:Text(timelist.title,
85
+ ),
86
+ ),
87
+ ),
88
+ ).toList(),
89
+ );
90
+ }),
91
+ );
92
+ }
93
+ );
94
+ }
95
+ }
96
+ ```
97
+ ```dart
98
+ class Time {
99
+
100
+ Time(DocumentSnapshot doc){
101
+ this.time = doc['time'];
102
+ this.title = doc['title'];
103
+ }
104
+ String time;
105
+ String title;
106
+ }
107
+ ```
108
+ ![イメージ説明](036b10cbf0908826e9101c08855df8d6.jpeg)]
109
+ ![イメージ説明](373d4265a892920f2ef52ec9998a74e0.jpeg)
110
+ usersのdocumentIDはuidです。

1

2021/06/19 14:09

投稿

kimidoro
kimidoro

スコア11

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,5 @@
1
1
  ### 問題点
2
- サインアップ時にfirebaseでuidをdocumentIDとして保存し、コレクショからデータを取得したい。
2
+ サインアップ時にfirebaseでuidをdocumentIDとして保存し、ログイしたユーザーのuidを用いてデータを取得したい。
3
3
 
4
4
  currentUserなどを使ってuidを取得し、検索に用いたいのですが、うまくいきませんでした。
5
5