kyed
kyed

Reputation: 61

Flutter - Delete item from list issue

I am trying to remove a items from a list OnPressed

The widget item looks like this


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]),
                                                            ),
                                                          ),

The void looks like this (the index and procedure have values):

void _deleteMaltFromList(index, procedure){
   
    print(index);
    print(procedure);

    setState(() {
      procedure.remove(index[index]);
    });
  }

This gives the error: Class 'int' has no instance method '[]'. Receiver: 0 Tried calling:

If I try for call the remove in the widget like below - I works fine

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

Upvotes: 0

Views: 390

Answers (2)

Kab Agouda
Kab Agouda

Reputation: 7289

List.remove()

The List.remove() function removes the first occurrence of the specified item in the list. This function returns true if the specified value is removed from the list.

List.remove(Object value) 

value − represents the value of the item that should be removed from the list. The following example shows how to use this function : Code :

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}'); 
}

Output :

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] 

You can learn more about here

Upvotes: 1

Randal Schwartz
Randal Schwartz

Reputation: 44186

If index is an int, then index[index] makes no sense, since int has no [] method. procedure clearly has a [] method, and succeeds both for .remove and [].

Upvotes: 1

Related Questions