I am trying the widgets.AnimatedGrid.1 mysample of AnimatedGrid class documentation and I am always getting a RangeError (index): Invalid value: Not in inclusive range whenever I replace at runtime the late ListModel<int> _list in _AnimatedGridSampleState with a new shorter list.
Simply replacing the code of _insert handler with:
void _insert() {
setState(() {
_list = ListModel<int>(
listKey: _gridKey,
initialItems: <int>[7, 6, 5],
removedItemBuilder: _buildRemovedItem,
);
});
}
then clicking on + button will throw a RangeError.
Since build() in AnimatedGridSampleState depends of _list I was expecting that it will build a new AnimatedGrid with the correct initialItemCount and avoiding RangeError:
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: ...,
body: Padding(
padding: const EdgeInsets.all(16.0),
child: AnimatedGrid(
key: _gridKey,
initialItemCount: _list.length,
itemBuilder: _buildItem,
),
),
),
);
}
Yet, the _buildItem(...) it is still being called with the same indexes of the former longer _list. Why?
You can try it by yourself running on the browser in the snippet container of AnimatedGrid page, replacing _insert() code just like shown in the following print screens. You will not see the RangeError but you will see that former items 4, 5, 6 remain on the AnimatedGrid.
