小部件库 RangeError (index) 捕获的异常:无效值:不在包含范围 0..35: 36
Exception caught by widgets library RangeError (index): Invalid value: Not in inclusive range 0..35: 36
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'Products',
),
),
body: Center(
child: FutureBuilder(
future: fetchPhotos(),
builder: (ctx, snapShot) {
if (snapShot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
} else {
return ListView.builder(
itemBuilder: (context, index) {
return ListTile(
leading: CircleAvatar(
backgroundColor: Colors.red,
),
title: Text(snapShot.data["table"][index]["name"]),
subtitle: Text(
"price: ${snapShot.data["table"][index]["class_id"]}"),
);
},
);
}
},
),
),
);
}
我应该在 itemCount 中放入什么才不会显示错误:小部件库 RangeError (index) 捕获异常:无效值:不在包含范围 0..35: 36
将 itemCount
参数添加到您的 listView Builder 中:
ListView.builder(
itemCount: snapShot.data["table"].length,
itemBuilder: (context, index) {
...
这样 flutter 就知道什么时候停止构建项目
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'Products',
),
),
body: Center(
child: FutureBuilder(
future: fetchPhotos(),
builder: (ctx, snapShot) {
if (snapShot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
} else {
return ListView.builder(
itemBuilder: (context, index) {
return ListTile(
leading: CircleAvatar(
backgroundColor: Colors.red,
),
title: Text(snapShot.data["table"][index]["name"]),
subtitle: Text(
"price: ${snapShot.data["table"][index]["class_id"]}"),
);
},
);
}
},
),
),
);
}
我应该在 itemCount 中放入什么才不会显示错误:小部件库 RangeError (index) 捕获异常:无效值:不在包含范围 0..35: 36
将 itemCount
参数添加到您的 listView Builder 中:
ListView.builder(
itemCount: snapShot.data["table"].length,
itemBuilder: (context, index) {
...
这样 flutter 就知道什么时候停止构建项目