RangeError(索引):无效值:不在包含范围0..1:2 en flutter

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

我有一个方法,它通过函数参数接收一个列表,然后显示它, 但是当我想在单击 x 图标时从列表中删除一个元素时, 我收到一个我不知道如何解决的错误。

不胜感激。

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

在 onPressed() 内部使用 setState();

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