Using flutter to display some content in a list shaped structure. I am getting data from a JSON file following this pattern:
[
{
"id":1,
"name": "foo",
"description":"foo description",
"effects": [
"foo effect"
]
},
...
{
"id":54,
"name": "foo 54",
"description":"foo description 54",
"effects": [
"foo effect 54"
]
}
]
I am using a ListView.builder
to display the content:
final List<String> itemsAssetnumber = [
"01", "02", "03", "04", "05", "06", "07", "08", "09",
"11", "12", "13", "14", "15", "16", "17", "18", "19",
"22", "23", "24", "25", "26", "27", "28", "29", "33",
"34", "35", "36", "37", "38", "39", "44", "45", "46",
"47", "48", "49", "55", "57", "58", "59", "66", "67",
"68", "69", "77", "78", "79", "88", "89", "99"];
ListView.builder(
itemCount: data.length, // length == 54
itemBuilder: (context, index){
return Card(
child: ListTile(
onTap: (){},
title: Text(data[index]['name']),
subtitle: Text(data[index]['description']),
leading: CircleAvatar(
backgroundImage: AssetImage('assets/items/${itemsAssetnumber[index]}.png')),
));
},
),
But I keep getting this stacktrace when I scroll to the end of the list see here:
════════ Exception caught by widgets library ═══════════════════════════════════
The following RangeError was thrown building:
RangeError (index): Invalid value: Not in inclusive range 0..52: 53
When the exception was thrown, this was the stack
#0 List.[] (dart:core-patch/growable_array.dart:166:60)
#1 ItemsScreen.build.<anonymous closure>
package:tft_app/screens/items_screen.dart:30
#2 SliverChildBuilderDelegate.build
package:flutter/…/widgets/sliver.dart:448
#3 SliverMultiBoxAdaptorElement._build.<anonymous closure>
package:flutter/…/widgets/sliver.dart:1136
#4 _HashMap.putIfAbsent (dart:collection-patch/collection_patch.dart:140:29)
...
════════════════════════════════════════════════════════════════════════════════
What am I missing?
question from:
https://stackoverflow.com/questions/65642645/flutter-rangeerror-index-invalid-value-not-in-inclusive-range-0-52-53-err