【问题标题】:In flutter How to save a list to firebase在颤振中如何将列表保存到firebase
【发布时间】:2020-11-14 23:15:20
【问题描述】:

我有一个包含偏移点的列表。

  List<Offset> _points = <Offset>[];

我正在尝试将列表保存到 firebase。

  floatingActionButton: new FloatingActionButton(
          child: new Icon(Icons.save),
        onPressed: ()=>Firestore.instance.collection('points').add({"point":_points.toString()}),

它作为字符串保存到 firebase

当我尝试另存为列表时

floatingActionButton: new FloatingActionButton(
          child: new Icon(Icons.save),
        onPressed: ()=>Firestore.instance.collection('points').add({"point":_points.toList()}),

它给出了以下错误

Invalid argument: Instance of 'Offset'

我需要将它保存为数组而不是字符串。我怎样才能完成它

【问题讨论】:

  • 您是否尝试过 ....add({"point":_points}) 。因为 _points 已经是一个列表
  • 是的,它给出了相同的错误偏移量实例@shababhsiddique

标签: firebase flutter


【解决方案1】:

这是因为您不能将对象列表保存为文档的字段(除非每个列表项都保存为文档),您通常可以将原始数据类型列表添加为文档的字段 (List&lt;String&gt;, List&lt;int&gt; ... ) , Offset 是一个不是有效参数的对象,因为它不是原语,这就是它抛出错误的原因。

【讨论】:

    【解决方案2】:

    通过@Aldy Yuan 提供的链接得到答案

    List<String> points1=[];
      List<String> toList1() {
    
        _points.forEach((item) {
          points1.add(item.toString());
        });
    
        return points1.toList();
      }
    

    然后我在 firestore 中添加 toList1

    onPressed: ()=>Firestore.instance.collection('points').add({"point":toList1()}),
    

    【讨论】:

      猜你喜欢
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      • 2021-04-03
      • 1970-01-01
      • 2022-11-12
      • 2019-10-29
      • 2021-10-09
      • 2019-07-30
      相关资源
      最近更新 更多