【发布时间】:2020-07-05 11:33:26
【问题描述】:
【问题讨论】:
标签: json listview flutter dart flutter-layout
【问题讨论】:
标签: json listview flutter dart flutter-layout
错误在您的 ListViewBuilder 中:
ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: _ListFamilyPageState.data.body.family.length,
itemBuilder: (context, index) {
return Container(
height: 74.0,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topRight: const Radius.circular(20.0),
bottomRight: const Radius.circular(20.0)),
),
width: MediaQuery.of(context).size.width - 50.0,
child: Center(
child: Text(
data.body.friends[index].id.toString(),
style:
TextStyle(fontSize: 24.0),
)));
},
)
您指定有family.length 个项目(在您的数据中:15),
但您正在从 friends[index] 中提取实际数据(您的数据中有 8 项)。
这会在渲染索引 8 处的项目时为您提供 RangeError。
最重要的是:您在自己的状态下使用静态数据:
class _ListFamilyPageState extends State<ListFamily> {
static Relations data;
// ...
}
不要那样做。
【讨论】:
您已使用 _ListFamilyPageState.data.body.family.length 设置 ListView 的 itemCount 属性,并且您已将其构建器的索引与另一个列表 data.body.friends[index].id.toString()
我认为两者的元素数量不同
【讨论】: