简体   繁体   English

我想从我的 Firestore 中获取文档 ID

[英]I want to get document id from my firestore

I can't get document id from firestore check out the screenshot, you will understand better.我无法从 Firestore 获取文档 ID 查看屏幕截图,您会更好地理解。

  Future updateUserData(String name, String address, String description,
      String image, String rating) async {
    return await collectionReference.document(uid).setData({
      'name': name,
      'address': address,
      'description': description,
      'image': image,
      'rating': rating,
      'berberid' : ######, // what should I write here 
    });
  }

Instead of null I want to pass that id我想传递该 ID 而不是 null

而不是 null 我想传递那个 id

Couple of method I already tried我已经尝试过的几种方法

This is returning null这是返回 null

String docid() {
    String id;
    collectionReference.getDocuments().then((value) => {
        value.documents.forEach((element) {
            id = element.documentID;
        }),  
    });

    return id;
}

This returns the name of collection, such as berbers这将返回集合的名称,例如 berbers

documentReference.documentID

Data is loaded from Firebase asynchronously.数据从 Firebase 异步加载。 That means that in this code:这意味着在这段代码中:

  String id;
  collectionReference.getDocuments().then((value) =>{
    value.documents.forEach((element) {
      id= element.documentID;
    }),          
  });
  return id;
}

By the time the return id statement executes, the id= element.documentID hasn't run yet.return id语句执行时, id= element.documentID还没有运行。 You can most easily check this for yourself by putting breakpoints on these lines and running the code in the debugger, or by adding some log statements:您可以通过在这些行上放置断点并在调试器中运行代码或添加一些日志语句来最轻松地自己检查这一点:

print("Before getDocuments()");
collectionReference.getDocuments().then((value) =>{
  print("Got documents");
});
print("Aftter getDocuments()");

When you run this code, it prints:当您运行此代码时,它会打印:

Before getDocuments()在 getDocuments() 之前

After getDocuments()在 getDocuments() 之后

Got documents拿到文件

This is likely not what you expected, but does explain why you get null from the function: the data hasn't loaded yet by the time your function exits.这可能不是您所期望的,但确实解释了为什么您从 function 获得 null:当您的 function 退出时,数据尚未加载。

This is completely normal when dealing with asynchronous operations, which is pretty much any modern web/cloud API.这在处理异步操作时是完全正常的,这几乎是任何现代 Web/云 API。 So you'll have to deal with it in your code.所以你必须在你的代码中处理它。

The quickest way to do that is to move the code that needs the data into the then() callback that gets the data.最快的方法是将需要数据的代码移动获取数据的then()回调中。 So instead of return id you actually put the code that uses the id into the method:因此,您实际上将使用 id 的代码放入方法中,而不是return id

  collectionReference.getDocuments().then((value) =>{
    String id;
    value.documents.forEach((element) {
      id= element.documentID;
    }),          
    print(id); // use the id here
  });

This could also be a call to your updateUserData function.这也可能是对您的updateUserData function 的调用。


Alternatively, you can use async / await and return a Future from your function:或者,您可以使用async / await并从 function 返回Future

Future<string> getDocumentId() async {
  String id;
  var value = await collectionReference.getDocuments();
  value.documents.forEach((element) {
    id= element.documentID;
  }),          
  return id;
}

To call this function you then do:要调用此 function,请执行以下操作:

getDocumentId().then((id) =>{
  print(id); // use id here
})

Or you can use another await and do:或者您可以使用另一个await并执行以下操作:

var id = await getDocumentId()
print(id);

Here's what I think you should do, you add the documentID to barberid when you create the document instead of updating it which is weird how the user know?.这是我认为您应该做的,在创建文档时将 documentID 添加到 barberid 而不是更新它,这很奇怪用户知道吗? Here's how you do it:这是你如何做到的:

  await Firestore.instance.collection("your collection name").add({
    'name': name,
    'address': address,
    'description': description,
    'image': image,
    'rating': rating,
  }).then((value) => Firestore.instance
      .document(value.path)
      .updateData({"barberid": value.documentID}));

value there is DocumentReference that's how you get the documentID and then you can update it with Firestore.instance.document("document path") fill the document path with value.path , and update the barberid with documentID should work just fine. value 有 DocumentReference ,这是您获取 documentID 的方式,然后您可以使用Firestore.instance.document("document path")对其进行更新,用value.path填充文档路径,并使用 documentID 更新 barberid 应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM