如何从flutter中使用Firestore中的唯一文档ID进行查询? [英] How to query using unique document id in Firestore from flutter?

查看:83
本文介绍了如何从flutter中使用Firestore中的唯一文档ID进行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个文档,其中包含不同的工作人员姓名及其详细信息,我没有给汽车分配一个唯一的ID,例如"id_1","id_2"等,而不是自动分配给每个文档.现在我的问题是如何访问这些不同的文档及其特定字段(如工作人员姓名和地址)并使用文本小部件显示它.

I have multiple documents containing different worker name and their details and instead of giving an auto I'd I have assigned a unique id like 'id_1', 'id_2' and so on to each documents. Now my question is how to access these different documents and its particular fields like worker name and address and display it using a text widget.

推荐答案

如果您想一次获取所有文档,则可以使用:

If u want to get all the documents at once then u can use :

StreamBuilder<QuerySnapshot>(
  stream: Firestore().collection('Workers').snapshots(),
  builder: (context, snapshot) {
    if (snapshot.data != null) {
      // Here u will get list of document snapshots
      final List<DocumentSnapshot> documents = snapshot.data.documents;
      // now u can access each document by simply specifying its number
      // u can also use list view to display every one of them
      return ListView.builder(
        itemCount: documents.length,
        itemBuilder: (context, int index) => Text(documents[index].data['name']),
      );
    } else {
      // Show loading indicator here
    }
  },
);

如果您想获取特定的文档详细信息(如果您具有文档ID),则可以使用:

If u want to get particular document details (if u have the document id) then u can use :

Future<DocumentSnapshot> _getDocument(String documentName) async {
   return await Firestore().collection('Workers').document(documentName).get();
 }

现在您可以通过名称访问字段了,例如

now u can access fields by there name e.g.

documentSnapshot.data['Name']

我希望这个帮助:)

这篇关于如何从flutter中使用Firestore中的唯一文档ID进行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆