Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 如何从Flatter中的Firestore检索每个文档的文档id_Firebase_Flutter_Dart_Google Cloud Firestore_Flutter Dependencies - Fatal编程技术网

Firebase 如何从Flatter中的Firestore检索每个文档的文档id

Firebase 如何从Flatter中的Firestore检索每个文档的文档id,firebase,flutter,dart,google-cloud-firestore,flutter-dependencies,Firebase,Flutter,Dart,Google Cloud Firestore,Flutter Dependencies,这里有一个代码,我从Firebase中检索所有文档,我只能检索文档中的数据,但我还想用它检索文档Id,我该怎么做 StreamBuilder<QuerySnapshot>( stream: getData(), builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) { if (snapshot.

这里有一个代码,我从Firebase中检索所有文档,我只能检索文档中的数据,但我还想用它检索文档Id,我该怎么做

StreamBuilder<QuerySnapshot>(
          stream: getData(),
          builder: (BuildContext context,
              AsyncSnapshot<QuerySnapshot> snapshot) {
            if (snapshot.hasError)
              return new Text('Error: ${snapshot.error}');
            switch (snapshot.connectionState) {
              case ConnectionState.waiting:
                return new Text('Loading...');
              default:
                return new ListView(
                  children: snapshot.data.documents
                      .map((DocumentSnapshot document) {
                    return new CustomCard(
                      name: document['name'],
                      phone: document['phno'],
                      service: document['Category'],
                      address: document['address'],
                      pin: document['pin'],
                      payment: document['Payment'],
                    );
                  }).toList(),
                );
            }
          },
        ),
StreamBuilder(
流:getData(),
生成器:(BuildContext上下文,
异步快照(快照){
if(snapshot.hasError)
返回新文本('Error:${snapshot.Error}');
交换机(快照.连接状态){
案例连接状态。正在等待:
返回新文本('加载…');
违约:
返回新的ListView(
子项:snapshot.data.documents
.map((文档快照文档){
退回新的海关卡(
名称:文件['name'],
电话:文件['phno'],
服务:文件[“类别”],
地址:文件[“地址”],
pin:document['pin'],
付款:单据[“付款”],
);
}).toList(),
);
}
},
),
这是我的getter函数

     Stream<QuerySnapshot> getData()async*{
    FirebaseUser user = await FirebaseAuth.instance.currentUser();
    yield* Firestore.instance
        .collection('User')
        .where("userId", isEqualTo: user.uid)
        .orderBy('upload time', descending: true)
        .snapshots();
  }
Stream getData()异步*{
FirebaseUser=等待FirebaseAuth.instance.currentUser();
yield*Firestore.instance
.collection('用户')
.where(“userId”,isEqualTo:user.uid)
.orderBy('上载时间',降序:true)
.快照();
}

要获取id,请执行以下操作:

 .map((DocumentSnapshot document) {
                    return new CustomCard(
                      docId : document.documentID,
                      name: document['name'],
                      phone: document['phno'],
                      service: document['Category'],
                      address: document['address'],
                      pin: document['pin'],
                      payment: document['Payment'],
                    );
                  }).toList(),

documentID
将为您提供每个文档id。

要获取id,请执行以下操作:

 .map((DocumentSnapshot document) {
                    return new CustomCard(
                      docId : document.documentID,
                      name: document['name'],
                      phone: document['phno'],
                      service: document['Category'],
                      address: document['address'],
                      pin: document['pin'],
                      payment: document['Payment'],
                    );
                  }).toList(),

documentID
将为您提供每个文档id。

您还可以执行以下操作

snapshot.data!.docs[index].id

这将返回该索引处的文档id,您还可以执行以下操作

snapshot.data!.docs[index].id
这将返回该索引处的文档id