Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

listview - Flutter: RangeError (index): Invalid value: Not in inclusive range 0..52: 53 Error even when item count is there

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...