RangeError (index): Недопустимое значение: Только допустимое значение — 0: 2
Я практикуюсь в получении API. Подскажите, пожалуйста, в чем проблема с моим кодом и как избежать появления этой ошибки красного экрана в моем приложении?
Это код-
RefreshIndicator(
onRefresh: () {
setState(() {});
return fetchOpenMenuList(widget.product);
},
child: FutureBuilder(
future: futureOpenMenuList,
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
padding: EdgeInsets.symmetric(vertical: 8.h),
itemCount: snapshot.data!.data.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
leading: Image.network(
snapshot.data!.data[index]!.items[index].thumb.toString()
),
title: Text(snapshot.data!.data[index]!.items[index].prodName),
subtitle: Text(snapshot.data!.data[index]!.name),
trailing: Container(
padding: EdgeInsets.all(5.r),
decoration: BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.circular(10.r),
),
child: Image.asset("assets/images/MenuIcon.png", height: 20.h, width: 20.w)
),
);
},
);
} else if (snapshot.hasError) {
return Center(child: Text('No Data Found'));
}
return const Center(
child: SizedBox(
height: 50.0,
width: 50.0,
child: CircularProgressIndicator(),
),
);
},
),
),
Это мой файл JSON-
{
"ignore": 0,
"code": 1,
"message": "OK",
"data": [
{
"ctg_id": "1",
"name": "Fusion",
"items": [
{
"prod_id": "1",
"prod_name": "Italian Sev Puri",
"thumb": "http:\/\/www.galacaterers.in\/images\/menu-items\/thumb-1459925145.jpg"
}
]
},
{
"ctg_id": "5",
"name": "Cake And Pastries",
"items": [
{
"prod_id": "57",
"prod_name": "Molt And Magic",
"thumb": "http:\/\/www.galacaterers.in\/images\/menu-items\/thumb-1459945416.jpg"
},
{
"prod_id": "49",
"prod_name": "Chocolate Zuzups",
"thumb": "http:\/\/www.galacaterers.in\/images\/menu-items\/thumb-1459945068.jpg"
}
]
},
{
"ctg_id": "6",
"name": "Chaat",
"items": [
{
"prod_id": "99",
"prod_name": "Makai Roll Chaat",
"thumb": "http:\/\/www.galacaterers.in\/images\/menu-items\/thumb-1459966275.jpg"
}
]
},
{
"ctg_id": "46",
"name": "Sweet Bite",
"items": [
{
"prod_id": "23",
"prod_name": "Fruit Wati",
"thumb": "http:\/\/www.galacaterers.in\/images\/menu-items\/thumb-1459942869.jpg"
}
]
}
]
}
Ниже приведен снимок экрана с ошибкой:

Чего я хочу:
[img]https://i.sstatic.net /rxQQ5m.png[/img]
Это модель данных-
import 'dart:convert';
MenuListData menuListDataFromJson(String str) => MenuListData.fromJson(json.decode(str));
String menuListDataToJson(MenuListData data) => json.encode(data.toJson());
class MenuListData {
MenuListData({
required this.ignore,
required this.code,
required this.message,
required this.data,
});
int ignore;
int code;
String message;
List data;
factory MenuListData.fromJson(Map json) => MenuListData(
ignore: json["ignore"],
code: json["code"],
message: json["message"],
data: List.from(json["data"].map((x) => Datum.fromJson(x))),
);
Map toJson() => {
"ignore": ignore,
"code": code,
"message": message,
"data": List.from(data.map((x) => x.toJson())),
};
}
class Datum {
Datum({
required this.ctgId,
required this.name,
required this.items,
});
String ctgId;
String name;
List items;
factory Datum.fromJson(Map json) => Datum(
ctgId: json["ctg_id"],
name: json["name"],
items: List.from(json["items"].map((x) => Item.fromJson(x))),
);
Map toJson() => {
"ctg_id": ctgId,
"name": name,
"items": List.from(items.map((x) => x.toJson())),
};
}
class Item {
Item({
required this.prodId,
required this.prodName,
required this.thumb,
});
String prodId;
String prodName;
String thumb;
factory Item.fromJson(Map json) => Item(
prodId: json["prod_id"],
prodName: json["prod_name"],
thumb: json["thumb"],
);
Map toJson() => {
"prod_id": prodId,
"prod_name": prodName,
"thumb": thumb,
};
}
Подробнее здесь: https://stackoverflow.com/questions/758 ... -2-flutter