Flutter-从列表问题中删除项目

时间:2020-10-21 21:29:28

标签: flutter

我正在尝试从OnPressed列表中删除项目

小部件项目如下


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 _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);
             });
             },
),
   ),
                                                                

2 个答案:

答案 0 :(得分:1)

如果indexint,则index[index]没有意义,因为int没有[]方法。 procedure显然具有[]方法,并且对于.remove[]都成功。

答案 1 :(得分:1)

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] 

您可以了解有关 here

的更多信息