繁体   English   中英

Append 一个数组到 Flutter Firestore

[英]Append an array into Flutter Firestore

我在 Flutter 中有一个List ,我想将里面的对象的 name 字段作为一个列表添加到一个名为requests的集合中
List selectedAllergies = [];
在此处输入图像描述

但是,当我这样做时,我收到此错误消息:
E/flutter ( 4031): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Invalid argument: Instance of 'Allergies'

List selectedAllergies = [
    Allergies(id: 1, name: "Milk"),
    Allergies(id: 2, name: "Peanuts"),
    Allergies(id: 3, name: "Tree Nuts"),
    Allergies(id: 4, name: "Eggs"),
    Allergies(id: 5, name: "Soy"),
    Allergies(id: 6, name: "Wheat"),
    Allergies(id: 7, name: "Shellfish"),
    Allergies(id: 8, name: "Snake"),
  ];
class Allergies {
  final int id;
  final String name;

  Allergies({
    required this.id,
    required this.name,
  });
}

是否可以将 append 只有selectedAllergies列表的“名称”字段放入requests集合中的文档中?

Firestore 不允许直接添加 dart 个对象。 在此处查看 Firetore 支持的数据类型列表。

相反,正如您所建议的,我们可以只添加过敏的名称。 以下代码说明了如何获取名称列表。

  List allergyNames = selectedAllergies.map((e) => e.name).toList();

然后,您可以将allergyNames添加到您的 Firestore 文档中。

暂无
暂无

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

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