本文介绍了获取userId后如何使用FutureBuilder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果要获取用户列表,只需将
设置 Firestore.instance.collection( users)。snapshots()
到Futurebuilder的未来:
If you want to get a list of users,just set Firestore.instance.collection("users").snapshots()
to the Futurebuilder's future:
但是,如何使用 userDataRef()
使用FutureBuilder根据userId获取用户数据?
But, how can I use userDataRef()
to get user's data based on userId using FutureBuilder?
Future<Stream<DocumentSnapshot>> userDataRef() async {
final FirebaseUser user = await auth.currentUser();
return Firestore.instance
.collection("users")
.document(user.uid).snapshots();
}
这是我的FutureBuilder。
尝试向用户显示用户最喜欢的食物。
This is my FutureBuilder.trying to display users favorite foods.
FutureBuilder(
future: Database.shared.userDataRef(),
builder: (BuildContext context, snapshot) {
if (!snapshot.hasData) {
return Text("Error");
}
final doc = snapshot.data.documents;
final List favoriteFoods = doc["favorite_foods"];
return Text(favoriteFoods.toString());
},
任何建议将不胜感激。
谢谢。
推荐答案
您可以使用嵌套的 FutureBuilder
如下:
You can use nested FutureBuilder
's like this :
Widget nestedFutureBuilders(){
return FutureBuilder(
future: YourFirstRequest,
builder: (BuildContext context, AsyncSnapshot<String> snapshotOne) {
switch (snapshotOne.connectionState) {
case ConnectionState.none:
return Text('Press button to start.');
case ConnectionState.active:
case ConnectionState.waiting:
return Text('Awaiting result...');
case ConnectionState.done:
if (snapshotOne.hasError)
return Text('Error: ${snapshotOne.error}');
return FutureBuilder<String>(
future: YourSecondRequest, // which is related the first request data (snapshotOne.data).
builder: (BuildContext context, AsyncSnapshot<String> snapshotTwo) {
switch (snapshotTwo.connectionState) {
case ConnectionState.none:
return Text('Press button to start.');
case ConnectionState.active:
case ConnectionState.waiting:
return Text('Awaiting result...');
case ConnectionState.done:
if (snapshotTwo.hasError)
return Text('Error: ${snapshotTwo.error}');
return BuildYourUIHere();// you have both data here(snapshotOne.data & snapshotTwo.data)
}
return null; // unreachable
},
);
}
return null; // unreachable
},
);
}
这篇关于获取userId后如何使用FutureBuilder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!