【问题标题】:RangeError (index): Invalid value: Not in range 0..7, inclusive: 8RangeError(索引):无效值:不在 0..7 范围内,包括:8
【发布时间】:2020-07-05 11:33:26
【问题描述】:

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

这是它目前显示的内容:

我正在为页面使用列表视图构建器。可以从此链接检索 JSON:Here

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

【问题讨论】:

    标签: json listview flutter dart flutter-layout


    【解决方案1】:

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

    不要那样做。

    【讨论】:

      【解决方案2】:

      您已使用 _ListFamilyPageState.data.body.family.length 设置 ListView 的 itemCount 属性,并且您已将其构建器的索引与另一个列表 data.body.friends[index].id.toString()

      我认为两者的元素数量不同

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-01
        • 2020-07-06
        • 1970-01-01
        • 1970-01-01
        • 2021-01-01
        • 1970-01-01
        • 2021-02-04
        • 2019-05-26
        相关资源
        最近更新 更多