Home > Software design >  Loop to add docs to Google Firestore
Loop to add docs to Google Firestore

Time:11-17

I am trying to loop through a List to add each item to a google firestore doc. However, the problem I am facing is that ONLY the last item in the List is added to the doc.

Note: The line print("=== $i POST SUCCESSFULLY ADDED ====") prints incremental for 5 times as expected

QUESTION: How can I get this loop to add all items to the firestore doc?

  void addPost() {
    DocumentReference documentReferencer = _firestore.collection('posts').doc();
    var i = 0;
    while (i < 5) {
      documentReferencer.set(postsData[i]);
      print("=== $i POST SUCCESSFULLY ADDED ===="); //PRINTS 5 TIMES
      i  ;
    }
  }

CodePudding user response:

I don't exactly know what your problem is in here, but it looks like your code is ignoring the fact that POST-ing data to a server is an asynchronous operation.

In other words, I'd expect that you need to await that to happen.

Also, I'd refactor the loop for readability.

Your code should be something like:

Future<void> addPost() async {
    DocumentReference documentReferencer = _firestore.collection('posts').doc();
    for(final post in postsData) {
      await documentReferencer.set(post);
      print("=== $i POST SUCCESSFULLY ADDED ===="); //PRINTS 5 TIMES
    }
  }

CodePudding user response:

In fact all items are written to the doc but you keep overwriting the document with the next item ending up with only the last item.

When calling set() you should use SetOptions to set merge to true like so.

  void addPost() {
    DocumentReference documentReferencer = _firestore.collection('posts').doc();
    var i = 0;
    while (i < 5) {
      documentReferencer.set(postsData[i], SetOptions(merge: true));
      print("=== $i POST SUCCESSFULLY ADDED ===="); //PRINTS 5 TIMES
      i  ;
    }
  }

Alternatively use update(). Attention: This will fail if the document does not exist yet.

  void addPost() {
    DocumentReference documentReferencer = _firestore.collection('posts').doc();
    var i = 0;
    while (i < 5) {
      documentReferencer.update(postsData[i]);
      print("=== $i POST SUCCESSFULLY ADDED ===="); //PRINTS 5 TIMES
      i  ;
    }
  }

I recommend you debugging your code step by step while keeping an eye on the database. Then you can see how you keep overwriting the doc.

Also be aware that you are not catching any errors in case the database calls go wrong. So you can't tell if the action was successful. The only thing you know at the point of your print() is that the request was send to Firebase.

Further readings:

  • Related