Flutter 上传列表到 Google Firestore [英] Flutter upload list to Google Firestore

查看:20
本文介绍了Flutter 上传列表到 Google Firestore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的 Flutter 测试应用中的列表添加到我的 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});
  });
}

一切正常,期待列表marked_friends"...

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

列表chipstuete"有多个字符串:

The list "chipstuete" has multiple strings:

[马丁·苏伯特、莉娜·赫斯勒、薇薇安·琼斯]

[Martin Seubert, Lena Hessler, Vivien Jones]

但我的 Firestore 看起来像这样:

But my Firestore looks like that:

目前整个列表存储在marked_friends[0]...

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

我需要更改什么,我的列表chipstuete"的每个条目都存储在 Firestore 中数组marked_friends"的单独字段中?

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?

最好的问候!

推荐答案

我有解决方案.

就像 SwiftingDuster 所说的,我需要一个新的方法来将它序列化为一个列表:

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();
}

之后,我只需在我的 postToFirestore() 方法中调用 toList() 并添加marked_friends":newtuete.就是这样!

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

这篇关于Flutter 上传列表到 Google Firestore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆