回答編集履歴
1
追加された内容に対して追記
test
CHANGED
@@ -1,3 +1,96 @@
|
|
1
|
+
Flutterで```Future```で情報を取り出した結果を利用するには```FutureBuilder```を使うのがいいです。
|
2
|
+
|
3
|
+
以下はこんな感じかなととりあえず書いた例です。データが確定しない間とかエラーだった場合の処理は適当です。
|
4
|
+
```dart
|
5
|
+
class Home extends StatelessWidget {
|
6
|
+
Future<String> future1() async {
|
7
|
+
String uid = FirebaseAuth.instance.currentUser!.uid;
|
8
|
+
String a = "";
|
9
|
+
await FirebaseFirestore.instance
|
10
|
+
.collection('users')
|
11
|
+
.doc(uid)
|
12
|
+
.get()
|
13
|
+
.then((DocumentSnapshot document) async {
|
14
|
+
Map<String, dynamic> data =
|
15
|
+
await document.data()! as Map<String, dynamic>;
|
16
|
+
a = await data['username'];
|
17
|
+
});
|
18
|
+
return a;
|
19
|
+
}
|
20
|
+
|
21
|
+
@override
|
22
|
+
Widget build(BuildContext context) {
|
23
|
+
return Scaffold(
|
24
|
+
body: Center(
|
25
|
+
child: Column(
|
26
|
+
mainAxisAlignment: MainAxisAlignment.start,
|
27
|
+
children: <Widget>[
|
28
|
+
Row(mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[
|
29
|
+
Container(child: Text('メニュー')),
|
30
|
+
]),
|
31
|
+
Container(
|
32
|
+
child: FutureBuilder(
|
33
|
+
builder: _buildChild,
|
34
|
+
future: future1(),
|
35
|
+
),
|
36
|
+
),
|
37
|
+
],
|
38
|
+
),
|
39
|
+
),
|
40
|
+
);
|
41
|
+
}
|
42
|
+
|
43
|
+
Widget _buildChild(BuildContext context, AsyncSnapshot<Object?> snapshot) {
|
44
|
+
if (snapshot.hasData) {
|
45
|
+
if (snapshot.data == "aaa") {
|
46
|
+
return Column(
|
47
|
+
mainAxisAlignment: MainAxisAlignment.center,
|
48
|
+
children: <Widget>[
|
49
|
+
Container(
|
50
|
+
margin: EdgeInsets.only(top: 100),
|
51
|
+
alignment: Alignment.center,
|
52
|
+
child: Text('はじめまして')),
|
53
|
+
]);
|
54
|
+
}
|
55
|
+
return Padding(
|
56
|
+
padding: EdgeInsetsDirectional.fromSTEB(0, 20, 0, 0),
|
57
|
+
child: Row(
|
58
|
+
mainAxisSize: MainAxisSize.max,
|
59
|
+
children: [
|
60
|
+
Padding(
|
61
|
+
padding: EdgeInsetsDirectional.fromSTEB(15, 0, 0, 0),
|
62
|
+
child: Container(
|
63
|
+
width: 280,
|
64
|
+
height: 100,
|
65
|
+
child: Column(
|
66
|
+
mainAxisSize: MainAxisSize.max,
|
67
|
+
children: [
|
68
|
+
Container(
|
69
|
+
width: 280,
|
70
|
+
height: 30,
|
71
|
+
child: Padding(
|
72
|
+
padding: EdgeInsetsDirectional.fromSTEB(0, 4, 0, 0),
|
73
|
+
child: Text(
|
74
|
+
'こんにちは',
|
75
|
+
textAlign: TextAlign.center,
|
76
|
+
),
|
77
|
+
),
|
78
|
+
),
|
79
|
+
],
|
80
|
+
),
|
81
|
+
),
|
82
|
+
),
|
83
|
+
],
|
84
|
+
),
|
85
|
+
);
|
86
|
+
} else {
|
87
|
+
// データが確定しない場合に表示するウィジェットの作成処理
|
88
|
+
return Text("hoge");
|
89
|
+
}
|
90
|
+
}
|
91
|
+
}
|
92
|
+
```
|
93
|
+
ーーーー
|
1
94
|
```await future1();```とするのが一番早いです。
|
2
95
|
|
3
96
|
ただ、たぶん```future1();```を呼び出す側が```async```がついてない関数なのでしょうか。
|
@@ -15,4 +108,3 @@
|
|
15
108
|
}
|
16
109
|
});
|
17
110
|
```
|
18
|
-
|