I am recording data coming from the user. I do need to get the document ID as soon as the document has been created. I have done some research and find this. But I am getting this error. Please, can you help me to fix this? Thank you.
CollectionReference users = FirebaseFirestore.instance
.collection('Users')
.doc(FirebaseAuth.instance.currentUser!.uid)
.collection('allTasks');
DocumentReference doc = await users
.add({
'task_Name' : _valueTaskNameSaved,
'project_Name': selectedProjectCapture,})
.then((value) => print("Task Created"))
.catchError((error) => print("Failed to add task: $error"));
CodePudding user response:
There is syntax error in your code.
Change from
DocumentReference doc = await users
.add({
'task_Name' : _valueTaskNameSaved,
'project_Name': selectedProjectCapture,)}
To this
DocumentReference doc = await users
.add({
'task_Name' : _valueTaskNameSaved,
'project_Name': selectedProjectCapture,})
CodePudding user response:
CollectionReference#add(T data) function returns an object of type Future<DocumentReference<T>>
. So there is no way you can save such an object into a variable of type DocumentReference. Actually, there is no need to save that at all. So to solve this, you can simply use:
users.add({
'task_Name' : _valueTaskNameSaved,
'project_Name': selectedProjectCapture})
.then((_) => print('Document added'))
.catchError((error) => print('Failed with: $error'));
CodePudding user response:
In fact, to make it work I have remove the end of the code. I have added // to the lines to remove. Now the error has disapeared
CollectionReference users = FirebaseFirestore.instance
.collection('Users')
.doc(FirebaseAuth.instance.currentUser!.uid)
.collection('allTasks');
DocumentReference doc = await users
.add({
'task_Name' : _valueTaskNameSaved,
'project_Name': selectedProjectCapture,})
// .then((value) => print("Task Created"))
// .catchError((error) => print("Failed to add task: $error"));