I have a listview.builder with data that you can click on and they will be added to the List, after this List I want to display on another screen (I do it through the provider). When clicking on the selected data for a long time, they should be deleted from the List, but I keep getting an error RangeError(index): Invalid value: Not in inclusive range 0..2: 5
my main screen
ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: searchMethodProvider.searchResult.length,
itemBuilder: (context, index) {
return Material(
color: Colors.transparent,
child: InkWell(
onLongPress: (){
setState(() {
searchMethodProvider.searchResult[index]['bool'] = true;
var modelApp = searchMethodProvider.marksList[index]['model'];
var indexApp = searchMethodProvider.marksList[index]['index'];
searchMethodProvider.addMark(indexApp, modelApp);
});
},
onTap: (){
setState(() {
searchMethodProvider.deleteDataIndex(index);
print(searchMethodProvider.dataMark);// searchMethodProvider.deleteDataIndex(index, context);
searchMethodProvider.searchResult[index]['bool'] = false;
});
},
child: Container(
height: 53,
width: double.infinity,
decoration: BoxDecoration(
border: Border(
top: BorderSide(
width: 0.8,
color: Color.fromRGBO(237, 237, 237, 1)
),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Align(
alignment: Alignment.centerLeft,
child: Padding(
padding: const EdgeInsets.only(
left: 14.22
),
child: Text(
searchMethodProvider.searchResult[index]['mark'],
style: TextStyle(
fontFamily: ConstantsFonts.sfProRegular,
fontSize: 16,
color: Color.fromRGBO(0, 0, 0, 1)
),
),
),
),
searchMethodProvider.searchResult[index]['bool'] == true ?
Icon(
Icons.keyboard_arrow_down,
size: 18,
color: Color.fromRGBO(87, 184, 238, 1),
) : Container()
],
),
),
),
);
}
)
this is my provider and him functions
class SearchMethodInMarkFilterProvider extends ChangeNotifier {
List<Map<String, dynamic>> marksList = [
{
'mark': 'Aston Martin',
'bool': false,
'index': 1,
'model': ['x5', '23']
},
{
'mark': 'Audi',
'bool': false,
'index': 2,
'model': ['x5', '23']
},
{
'mark': 'BMW',
'bool': false,
'index': 3,
'model': ['x5', '23']
},
];
List dataMark = [];
Map addData = {};
List<Map<String, dynamic>> searchResult = [];
void addMark(int indexApp, List markData){
addData = {
'indexApp': indexApp,
'markData': markData,
};
dataMark.add(addData);
print(dataMark);
}
void deleteDataIndex(int index){
dataMark.removeAt(index);
notifyListeners();
}
SearchMethodInMarkFilterProvider(){
searchResult = marksList;
}
void runFilter(String enteredKeyword) {
List<Map<String, dynamic>> results = [];
if (enteredKeyword.isEmpty) {
results = marksList;
} else {
results = marksList
.where((user) =>
user['mark'].toLowerCase().contains(enteredKeyword.toLowerCase()))
.toList();
}
searchResult = results;
notifyListeners();
}
}
maybe I'm creating the data model incorrectly, that's why I can't delete it properly, I'll be glad of any help
2
Answers
You can also adjust the starting and ending dates, as well as the starting row number, to suit your needs.
I think it will help you.
This happened because your
listview
build on thesearchResult
list, but you try use its index onmarksList
. You have two options, either make list withmarksList
or change your delete function and do that onsearchResult
.