【发布时间】:2021-08-24 05:12:31
【问题描述】:
在 Firestore 中,有一些文档包含 'name' 、 'continent' 等字段及其字符串值。我在主 UI 代码中使用 FutureBuilder,并调用 getData() 函数。
这是我的代码
class DatabaseService {
final locationCollection = FirebaseFirestore.instance.collection("location");
Future<List<HomePageModel>> getData() async {
List<HomePageModel> listData = [];
await locationCollection.get().then((QuerySnapshot querySnapshot){
querySnapshot.docs.forEach((doc) {
HomePageModel(
docId: doc.id,
continent: doc['continent'],
name: doc['name']
);
});
});
print("this is listData $listData");
return listData;
}
}
这是控制台的样子:
I/flutter ( 430): this is listData []
这是 HomePageModel 类
class HomePageModel {
String? name;
String? docId;
String? continent;
HomePageModel({this.docId, this.name, this.continent});
}
我不知道如何将这些从 Firestore 检索到的文档数据模型放在 listData 中,这就是为什么在控制台中 listData 没有任何数据。
谢谢你:)
【问题讨论】:
-
你能详细说明一下吗,用代码sn-p..我是初学者
标签: firebase flutter dart google-cloud-firestore