我正试图从按下的列表中删除一个项目
小部件项目看起来像这个
Flexible(
flex: 1,
fit: FlexFit.tight,
child: IconButton(
iconSize: 16.0,
color: Colors.green[800],
icon: Icon(Icons.delete),
onPressed: () => _deleteMaltFromList(gram[index],colors[index],maltname[index], index, procedure[index]),
),
),
void看起来是这样的(索引和过程都有值(:
void _deleteMaltFromList(index, procedure){
print(index);
print(procedure);
setState(() {
procedure.remove(index[index]);
});
}
这会产生错误:类"int"没有实例方法"[]"。接收器:0尝试调用:
如果我尝试调用下面这样的小部件中的删除-我工作得很好
Flexible(
flex: 1,
fit: FlexFit.tight,
child: IconButton(
iconSize: 16.0,
color: Colors.green[800],
icon: Icon(Icons.delete),
onPressed: (){
setState(() {
procedure.remove(procedure[index], index);
});
},
),
),
如果index
是int
,则index[index]
没有意义,因为int
没有[]
方法。procedure
显然具有[]
方法,并且对.remove
和[]
都是成功的。
List.remove((
List.remove()
函数删除列表中指定项目的第一次出现。如果从列表中删除了指定的值,此函数将返回true。
List.remove(Object value)
值−表示应从列表中删除的项目的值。以下示例显示了如何使用此函数:代码:
void main() {
List l = [1, 2, 3,4,5,6,7,8,9];
print('The value of list before removing the list element ${l}');
bool res = l.remove(1);
print('The value of list after removing the list element ${l}');
}
输出:
The value of list before removing the list element [1, 2, 3, 4, 5, 6, 7, 8, 9]
The value of list after removing the list element [2, 3, 4, 5, 6, 7, 8, 9]
您可以在此处了解有关的更多信息