从Firestore中获取数据,并用它来使将来的构建者制作卡片?

时间:2020-06-17 18:20:36

标签: firebase flutter dart google-cloud-firestore

这是数据库类。我正在尝试从fetchprojects函数中获取数据到将来的构建器中,但无法做到这一点。

List<Project> fetchProjects(QuerySnapshot snapshot) {
    return snapshot.documents.map((e) {
        return Project(
            id: e.data['id'],
            title: e.data['title'],
            description: e.data['description'],
            prequisites: e.data['prequisites'],
            complexity: e.data['complexity'],
            affordability: e.data['affordability'],
            duration: e.data['duration'],
            members: e.data['members'],
            contact: e.data['contact']);
        }).toList();
    }
    // get brews stream
    //
    Stream<List<Project>> get projects {
        return projectCollection.snapshots().map(fetchProjects);
    }
}

1 个答案:

答案 0 :(得分:0)

感谢您的帮助!此代码段对我有用。您可以通过返回listviewBuilder而不是center来创建卡片。

Widget build(BuildContext context) {

return StreamBuilder(

  stream: Firestore.instance.collection('projects').snapshots(),
   builder: (context,snapshot){


     if(!snapshot.hasData)
     return Text('data is loading');
     return Center(child: Text(snapshot.data.documents[0]['title']),);
   }
);}