Home > Net >  RangeError (index): Invalid value: Not in inclusive range 0..1: 2 en flutter
RangeError (index): Invalid value: Not in inclusive range 0..1: 2 en flutter

Time:05-13

I have a method that receives a list by the function parameter and then I show it, but when I want to remove an element from the list when I click on the x icon, I get an error that I don't know how to solve.

Help would be greatly appreciated.

  Widget HistorialB(List <SearchDelegateModel> historialMenuPrincipal ){
         
           return Container(
             
         decoration: BoxDecoration(
                
         borderRadius: BorderRadius.circular(12),
              
          color: Colors.white
              ),
              child: ListView.builder(
                  itemCount: historialMenuPrincipal.length,
              
                  itemBuilder: (context,i)
                  {
                    contentPadding: EdgeInsets.symmetric(vertical: 12,horizontal: 16);
                    leading:CircleAvatar(
                    radius: 32,
                    backgroundImage: NetworkImage(
                    "https://2.bp.blogspot.com/-3ZzNt8ZsjQk/WR9W4IFn4II/AAAAAAAAAJw/_inTVynhS60V7F5IZ-461-pda7WArTStwCEw/s1600/ANA.jpg"),
                    );
                    return
                      ListTile(
                      title: Text(historialMenuPrincipal[i].email?? ""), 
              
                      trailing: IconButton(
                       icon: Icon(Icons.cancel,color: Colors.black,),
                        onPressed: () {
          
                         historialMenuPrincipal.remove(historialMenuPrincipal[i]);
                          (context as Element).markNeedsBuild();
                
                          
                        },
                      ),
                    
                    );
                  }
              ),
        
            );
        
        }

CodePudding user response:

Inside onPressed() use setState();

onPressed: () {
          
       setState(() {  
                historialMenuPrincipal.remove(historialMenuPrincipal[i]);
                          (context as Element).markNeedsBuild();
                
                       });
                        },
  • Related