Param B.
我觉得problem在这儿。
await locationCollection.get().then((QuerySnapshot querySnapshot){
querySnapshot.docs.forEach((doc) {
HomePageModel(
docId: doc.id,
continent: doc['continent'],
name: doc['name']
);
});
});
第一个问题是您没有创建新的对象实例。第二个是您没有将新对象实例添加到列表中。
我没有测试代码,但我认为这会起作用。
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 homepagemodel = new HomePageModel( // the first issue
docId: doc.id,
continent: doc['continent'],
name: doc['name']
);
listData.add(homepagemodel); // the second issue
});
});
return listData;
}
}
还有一件事。我真的不知道使用之间有什么区别{
}
and ?
在课堂上,但我从未使用过它。我开设了这样的课程。但我认为你的课也有效。
class HomePageModel {
String name;
String docId;
String continent;
HomePageModel(this.docId, this.name, this.continent);
}
另一件事是,打印不适用于项目列表。例如,您需要使用for loop
像这样。
for (var i = 0; i < listData.length; i++) {
print("Loaded data: " + listData[i].name);
}