Reputation: 13
I can't get document id from firestore check out the screenshot, you will understand better.
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
Couple of method I already tried
This is returning 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
documentReference.documentID
Upvotes: 0
Views: 754
Reputation: 2045
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 ?. 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.
Upvotes: 1
Reputation: 598603
Data is loaded from Firebase asynchronously. 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. 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()
After 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.
This is completely normal when dealing with asynchronous operations, which is pretty much any modern web/cloud 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. So instead of return id
you actually put the code that uses the id into the method:
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.
Alternatively, you can use async
/ await
and return a Future
from your function:
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:
getDocumentId().then((id) =>{
print(id); // use id here
})
Or you can use another await
and do:
var id = await getDocumentId()
print(id);
Upvotes: 2