【发布时间】:2019-08-13 09:57:13
【问题描述】:
我正在使用 firebase,需要在我的 initState() 方法中获取用户的 id,因为我正在构建一个需要用户 id 的小部件,来自 firebase。 firebase 使用的currentUser 方法是Future。
目前,我将此作为我的初始化状态和getUser 函数:
Future<FirebaseUser> getUser() async {
FirebaseUser user = await FirebaseAuth.instance.currentUser();
return user;
}
void initState() {
getUser().then((result) {
stream = Firestore.instance.collection('Lessons').where("teacher", isEqualTo: result.uid).limit(4).snapshots();
_AppBarOptions = <Widget>[
AppBar(
title: new Text('Dashboard', style: TextStyle(color: Colors.white)),
backgroundColor: Colors.white,
),
AppBar(
title: new Text('Lessons', style: TextStyle(color: Colors.white))),
AppBar(title: new Text('Tasks', style: TextStyle(color: Colors.white))),
AppBar(
title: new Text('Settings', style: TextStyle(color: Colors.white)))
];
_widgetOptions = <Widget>[
Text(
'Dashboard will go here',
style: optionStyle,
),
StreamBuilder(
stream: stream,
builder: (context, snapshot) {
if(snapshot.hasData) {
return ListView.builder(
itemExtent: 80.0,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) =>
_buildListItem(context, snapshot.data.documents[index]),
);
} else {
return Text('You have no Lessons');
}
}),
Text(
'Tasks will go here',
style: optionStyle,
),
Container(
child: new RaisedButton(
child: new Text('Sign Out'),
onPressed: signOut,
),
)
];
});
}
我面临的问题是build方法在这个完成之前执行,它使用widgetOptions来显示文本,导致错误。
我想知道是否有任何方法可以改进这一点,以便能够获取用户的 id 并在stream 中使用它,以便它可以获取文档?
非常感谢。
【问题讨论】: