gpt4 book ai didi

firebase - 数组的元素是固定长度的,而数组本身不是

转载 作者:行者123 更新时间:2023-12-03 04:19:47 26 4
gpt4 key购买 nike

var data = List();
snapshot.data.documents.forEach((doc) => data.add(doc.data)); //my example data is one doc
data.add({"x": "y"}); //works
data[0]["bar"].add({"foo": "bar"}); //doesn't
我的数据如下所示:
  data = [
{
"foo": "",
"bar": [
{"foo": "bar"},
{"foo": "bar"},
{"foo": "bar"},
],
},
];
当我进行第二次数组修改时,它给了我这个错误:
Unhandled Exception: Unsupported operation: Cannot add to a fixed-length list
我尝试使用未从firebase提取的普通数据执行此操作,并且按预期工作。我也尝试使用List.from来填充数组,但是它也不起作用。

最佳答案

snapshot.data.documents[0]['bar']显然是固定长度的(即,不可增长的)List。您不能使List不可增长。相反,您需要创建一个可增长的副本:

data[0]["bar"] = data[0]["bar"].toList(); // Create a copy.
data[0]["bar"].add({"foo": "bar"}); // Now this should work.
或者:
data[0]["bar"] = [...data[0]["bar"], {"foo": "bar"}];
但是,尽管后一种方法更为紧凑,但不清楚是否明确需要该副本。

关于firebase - 数组的元素是固定长度的,而数组本身不是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63385782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com