简体   繁体   中英

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:

在此处输入图片说明

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!

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(),

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM