json - RangeError(索引):无效值:不在0..7范围内,包括8

标签 json listview flutter dart flutter-layout

我正在尝试显示从API请求中检索到的所有值。

这是当前显示的内容:

我正在为页面使用listview构建器。可以从以下链接中检索JSON:Here

这是我的整个页面代码:Code

最佳答案

错误出现在您的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;
  // ...
}

不要那样做

关于json - RangeError(索引):无效值:不在0..7范围内,包括8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60840078/

相关文章:

iOS : JSON String to NSArray not working as expected

json - Flutter 迁移到 null 安全性,是迟到还是可为 null?

java - 从 JSON 响应中提取值

java - Android - 使用 Volley 显示自定义 ListView (使用 http 请求发送 header )

ios - CDN : trunk URL couldn't be downloaded: https://cdn. cocoapods.org/CocoaPods-version.yml 响应:403

php - 使用 PHP 解码 JSON

ios - Xamarin表单-所选项目的Listview将所有背景更改为相同

jquery mobile listview隐藏和显示然后无法搜索

validation - 在Flutter小部件测试中,如何验证字段验证错误消息?

flutter - flutter 中是否可以有这样的小部件