List 如何在Firestore的Flatter中创建列表?
我是个新手。 我如何从Firestore中检索数据,并与集合中的每个孩子形成新的配置文件?第二个问题是如何在另一个dart文件中使用此列表?非常感谢。 多谢各位List 如何在Firestore的Flatter中创建列表?,list,flutter,google-cloud-firestore,List,Flutter,Google Cloud Firestore,我是个新手。 我如何从Firestore中检索数据,并与集合中的每个孩子形成新的配置文件?第二个问题是如何在另一个dart文件中使用此列表?非常感谢。 多谢各位 final List<Profile> demoProfiles = [ Profile ( photos: [ "https://", ], name: "abc", ), Profile ( photos: [ "https://", ], name: "abc", ) ]; 假设您有这样的f
final List<Profile> demoProfiles = [
Profile (
photos: [
"https://",
],
name: "abc",
),
Profile (
photos: [
"https://",
],
name: "abc",
)
];
假设您有这样的firestore结构:
Profile
photos: []
name: ""
age: ""
distance: ""
education: ""
您可以使用以下代码段获取数据并将其构建到对象中:
fetchData() async{
final db = await Firestore.instance;
List<Profile> demoProfiles = []
db.collection("Profile").get().then(function(snapshot){
snapshot.forEach((document) {
demoProfiles.add(Profile(
photos: document.data.photos,
name: document.data.name,
age: document.data.age,
distance: document.data.distance,
education: document.data.education
))
})
})
}
编辑:
1从profiles类中删除mockedup配置文件列表,它不应该在那里
2将主控制器编辑为以下内容:
class _MainControllerState extends State<MainController> {
List<Profile> demoProfiles = [];
demoProfiles = fetchData();
Final MatchEngine match Engine = MatchEngine (
matches:demoProfiles.map((Profile profile) => Match(profile: profile)).toList();
);
fetchData() async{
final db = await Firestore.instance;
List<Profile> list = [];
db.collection("Profile").get().then(function(snapshot){
snapshot.forEach((document) {
list.add(Profile(
photos: document.data.photos,
name: document.data.name,
age: document.data.age,
distance: document.data.distance,
education: document.data.education
))
})
});
return list;
}
}
谢谢你的帮助,但我对颤振真的是个新手,粘胶公司把这个错误还给了我。我怎样才能解决这个问题?等待表达式只能在异步函数中使用。尝试将函数体标记为“async”或“async*”。dartawait_在错误上下文中“await”应用于“Firestore”,这不是“未来”。dartawait_仅用于未来将异步添加到包含此代码的函数中,如此fetchdata async{},我会将其编辑到答案中。我上传了一些代码的屏幕截图,您能告诉我如何正确添加异步吗?非常感谢,你救了我。问题是你把代码放在了错误的类中,我在答案中共享的代码不应该在你的配置文件类中执行,但可能在你的控制器(更可能是控制器)或引擎上执行,这取决于你的应用程序的结构。我不知道我应该把你的代码放在哪里。我更新了原始工作代码,我想用Firestore中的数据更新列表。我怎么做?