如何在Firestore中快速创建列表<>?

时间:2020-05-20 20:10:45

标签: list flutter google-cloud-firestore

我是新手。 如何从Firestore检索数据并与集合中的每个孩子一起形成新的个人资料?第二个问题是如何在另一个Dart文件中使用此列表?谢谢 谢谢。

final List<Profile> demoProfiles = [
Profile (
photos: [
     "https://",
 ],
name: "abc",

),
Profile (
photos: [
  "https://",

],
name: "abc",

)
 ];

Profile.dart enter image description here

1 个答案:

答案 0 :(得分:1)

假设您有一个这样的消防站结构:

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)从个人档案类中删除个人档案的模型列表,该列表不应存在

2)将mainController编辑为以下内容:

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;
    }

}