Martin Seubert
Martin Seubert

Reputation: 1018

Flutter upload list to Google Firestore

I would like to add a list from my flutter test app to my Google Firestore.

This is my method, which adds all the data:

void postToFireStore(
{String mediaUrl, String location, String description}) {

var reference = Firestore.instance.collection('insta_posts');

  reference.add({
    "marked_friends": [chipstuete.toString()],
    "username": currentUserModel.username,
    "location": location,
    "likes": {},
    "mediaUrl": mediaUrl,
    "description": description,
    "ownerId": googleSignIn.currentUser.id,
    "timestamp": new DateTime.now().toString(),

    }).then((DocumentReference doc) {
    String docId = doc.documentID;
    reference.document(docId).updateData({"postId": docId});
  });
}

Everything is working fine, expect the list "marked_friends"...

The list "chipstuete" has multiple strings:

[Martin Seubert, Lena Hessler, Vivien Jones]

But my Firestore looks like that:

enter image description here

At the moment the whole list is stored in marked_friends[0]...

What do I need to change, that every entry of my list "chipstuete" is stored in a seperate field of my array "marked_friends" in Firestore?

Best regards!

Upvotes: 3

Views: 5422

Answers (2)

Martin Seubert
Martin Seubert

Reputation: 1018

I have the solution.

Like SwiftingDuster said, I needed a new method which serializes it to a List:

List<String> toList() {

  chipstuete.forEach((item) {
    newtuete.add(item.toString());
  });

  return newtuete.toList();
}

After that I just call toList() in my postToFirestore() Method and add "marked_friends": newtuete. Thats it!

Upvotes: 3

Swift
Swift

Reputation: 3410

You have to add a method in your AppProfile class that serializes it to a List.

So in your AppProfile class:

class AppProfile {

  ... // Whatever fields/methods you have

  // Add this method
  List<String> getFriendList() {
    // Somehow implement it so it returns a List<String> based on your fields
    return ['name1','name2','name3'];
  }
}

Then you can do

"marked_friends": chipstuete.getFriendList(),

Upvotes: 3

Related Questions