我可以使用此快照调用访问文档ID吗?

时间:2020-06-20 07:10:47

标签: firebase flutter dart google-cloud-firestore snapshot

这是streambuilder im用于一次访问所有文档的

 StreamBuilder(
        stream: Firestore.instance.collection('projects').snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) return Text('data is loading');
          return ListView.builder(
            itemBuilder: (ctx, index) {
              return ProjectItem(
                id: snapshot.data.documents[index]['id'],
                title: snapshot.data.documents[index]['title'],
                members: snapshot.data.documents[index]['members'],
                complexity: snapshot.data.documents[index]['complexity'],
                affordability: snapshot.data.documents[index]['affordability'],
                duration: snapshot.data.documents[index]['duration'],
                docid: snapshot.data.document[index].documentid,


              );
            },
            itemCount: snapshot.data.documents.length,
}

我可以使用docid:snapshot.data.document [index] .documentid单独访问文档ID吗?如果不是,那有什么选择呢?谢谢

3 个答案:

答案 0 :(得分:2)

您可以创建一个自定义的Object构造函数以使用文档快照中的数据。

class ProjectItem {
  String id;
  String title;
  String members;
  String complexity;
  String affordability;
  String duration;
  String docid;

  ProjectItem({....});

  factory ProjectItem.fromFirestore(DocumentSnapshot doc) {
    return ProjectItem(
      id: doc.data['id'],
      title: doc.data['title'],
      members: doc.data['members'],
      complexity: doc.data['complexity'],
      affordability: doc.data['affordability'],
      duration: doc.data['duration'],
      docid:  doc.documentID,
    );
  }
}

然后只需在列表视图中使用此构造函数即可制作对象

return ProjectItem.fromFirestore(
   snapshot.data.documents[index]
);

答案 1 :(得分:0)

您可以通过这种方式获取documentID

StreamBuilder(
        stream: Firestore.instance.collection('projects').snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) return Text('data is loading');
          return ListView.builder(
            itemBuilder: (ctx, index) {
              QuerySnapshot snap = snapshot.data; // Snapshot
              List<DocumentSnapshot> items = snap.documents; // List of Documents
              DocumentSnapshot item = items[index]; Specific Document
              return ProjectItem(
                id: item.data['id'],
                title: item.data['title'],
                members: item.data['members'],
                complexity: item.data['complexity'],
                affordability: item.data['affordability'],
                duration: item.data['duration'],
                docid: item.documentID, // Document ID
              ); 
            },
            itemCount: snapshot.data.documents.length,
          );
        },
      ),
    )

答案 2 :(得分:0)

这对我有用:

StreamBuilder(
    stream: Firestore.instance.collection('projects').snapshots(),
    builder: (context, snapshot) {
      if (!snapshot.hasData) return Text('data is loading');
      return ListView.builder(
        itemBuilder: (ctx, index) {
          return ProjectItem(
            id: snapshot.data.documents[index]['id'],
            title: snapshot.data.documents[index]['title'],
            members: snapshot.data.documents[index]['members'],
            complexity: snapshot.data.documents[index]['complexity'],
            affordability: snapshot.data.documents[index]['affordability'],
            duration: snapshot.data.documents[index]['duration'],
            docid: snapshot.data.documents[index].documentID,
            
            
          );
        },
        itemCount: snapshot.data.documents.length,
      );