NewbieProgrammer
NewbieProgrammer

Reputation: 142

How to add data as subcollection to all user's document in firebase flutter?

These are the users from my 'LoanFriends' collection:

enter image description here

And these are the data that I want to add inside of each user.

enter image description here

This is my Cloud Firestore screenshot:

enter image description here

how do I create different data sub-collections for each user that I add? What I did right now resulted in me getting the same data for different users which is not right.

Upvotes: 1

Views: 1046

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 139039

According to my question:

Do you want to add subcollections for the existing data or for the new data?

And your answer:

ok, for the existing data.

To solve this, you have to iterate the entire "LoanFriends" collection, read the documents, and for each document that you find, you have to make a .collection('ReceivedLoanData') and .collection('GaveLoanData') call. Right after that, chain a add() function call to add the desired documents right under each subcollection.

Edit:

According to your last comment:

Can you explain what I had to do if it were for a new data

Then you have to create another reference:

var receivedLoanDataRef = db.collection("LoanFriends").document(docId)
                            .collection("ReceivedLoanData");

And add the data a accordingly:

receivedLoanDataRef.add(data);

Upvotes: 3

Related Questions