skip to Main Content

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"));

2

Answers


  1. Chosen as BEST ANSWER

    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"));
    

  2. 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'));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search