【问题标题】:How can i get the data from Firestore without streambuilder? [closed]如何在没有流构建器的情况下从 Firestore 获取数据? [关闭]
【发布时间】:2020-03-14 19:02:31
【问题描述】:
我想用 firestore 中的值设置卡片,如果没有 streambuilder,我怎么能得到它?
class _CardsSectionState extends State<CardsSectionAlignment>
with SingleTickerProviderStateMixin {
int cardsCounter;
int card = 0;
【问题讨论】:
标签:
firebase
flutter
dart
google-cloud-firestore
【解决方案1】:
您需要在_CardsSectionState 的initState 方法中执行查询,然后将查询结果中的卡片值赋给您的整数变量card。
将 collectionName 替换为您拥有卡片值的集合名称。将“card_value”替换为集合中卡片值所在的字段名称。
@override
void initState() {
super.initState();
Firestore.instance.collection('collectionName').getDocuments().then((val){
if(val.documents.length > 0){
card = val.documents[0].data["card_value"];
}
else{
print("Not Found");
}
});
}
【解决方案2】:
要能够为card 设置值,您需要使用将返回Stream 的snapshot() 方法并继续侦听更新,或者您需要使用将返回的get() Future。在这两种情况下,操作都是异步的。因此不使用streambuilder就无法设置它