简体   繁体   English

将列表上传到Google Firestore

[英]Flutter upload list to Google Firestore

I would like to add a list from my flutter test app to my Google Firestore. 我想将Flutter测试应用中的列表添加到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"... 一切正常,请期待列表“ marked_friends” ...

The list "chipstuete" has multiple strings: 列表“ chipstuete”具有多个字符串:

[Martin Seubert, Lena Hessler, Vivien Jones] [Martin Seubert,Lena Hessler和Vivien Jones]

But my Firestore looks like that: 但是我的Firestore看起来像这样:

在此处输入图片说明

At the moment the whole list is stored in marked_friends[0]... 目前,整个列表存储在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? 我需要更改什么,将列表“ chipstuete”的每个条目存储在Firestore中数组“ marked_friends”的单独字段中?

Best regards! 最好的祝福!

You have to add a method in your AppProfile class that serializes it to a List . 您必须在AppProfile类中添加一个将其序列化为List

So in your AppProfile class: 因此,在您的AppProfile类中:

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: 就像SwiftingDuster所说的那样,我需要一个新方法将其序列化为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. 之后,我只需在我的postToFirestore()方法中调用toList()并添加“ marked_friends”:newtuete。 Thats it! 而已!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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