gpt4 book ai didi

flutter - 如何在指定索引中增加值(value)?

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

我有dataSet的列表。

List<Model> list = [];

...

有一些将数据添加到列表的过程。

我想将索引传递给该方法。我要替换数据。

我尝试了这3种方法,但没有替换..添加另一个新值。
list.insert(widget.configureIndex, model);//1

list[widget.configureIndex] = model;//Error: RangeError (index): Invalid value: Only valid value is 0: 1

list.replaceRange(widget.configureIndex, widget.configureIndex, [model]);//3

最佳答案

我认为您遇到的问题是您的列表在您要替换的索引处没有值。

List<Model> list = [];
int configureIndex = 2;
// None of these work because your list is empty to begin with.
emptyList.insert(configureIndex, model);
emptyList[configureIndex] = model;
emptyList.replaceRange(configureIndex, configureIndex, [model]);


// Your list must have a value at the index you want to replace

list = [model, model, model]; // list now has value at index 2
if(list.length > configureIndex) { // These will work!
emptyList.insert(configureIndex, model);
emptyList[configureIndex] = model;
emptyList.replaceRange(configureIndex, widget.configureIndex, [model]);
}

关于flutter - 如何在指定索引中增加值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59062783/

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