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

can i access the document id using this snapshot call?

This is the streambuilder im using to access all the documents at once

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

Can i use docid: snapshot.data.document[index].documentid to access the document ids individually? if not whats the option? Thanks

你可以这样获取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,
          );
        },
      ),
    )

您可以制作自定义对象构造函数以使用来自文档快照的数据。

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]
);

THIS WORKED FOR ME:

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,
      );