flutter - RangeError(索引): Invalid value: Not in range 0. .8,包括 : 9 in Gridview. 计数

标签 flutter dart

我找到了解决方案 ListView.builder“您应该将 itemCount 参数传递给 ListView.builder 以允许它知道项目计数”但不适用于 GridView.count。

抛出另一个异常:RangeError (index): Invalid value: Not in range 0..8, inclusive: 9

enter image description here

import 'package:thunder_mobile/screens/dashboard-page/common-list-page/common_list.dart';
import 'package:thunder_mobile/screens/dashboard-page/parent-views/materials/material_list.dart';
import 'package:thunder_mobile/utils/all_api_calls.dart';
import 'package:thunder_mobile/widgets/app-bar/app_bar_tabs.dart';
import 'package:thunder_mobile/widgets/icons/thunder_svg_icons.dart';
import 'package:thunder_mobile/widgets/loading/custom-loading.dart';
import 'teacher_homework_classes_modal.dart';

class SubjectWiseHomework extends StatefulWidget {
  final String title;

  const SubjectWiseHomework({Key key, this.title}) : super(key: key);
  @override
  State<StatefulWidget> createState() {
    return new GridViewSubjectsState();
  }
}

class GridViewSubjectsState extends State<SubjectWiseHomework> {
  List<SubjectList> subjectList;
  var _isLoading = true;
  var jsonApiService = JsonApiHelper();
  @override
  void initState() {
    super.initState();
    getSubjectList();
  }

  getSubjectList() {
    jsonApiService.fetchMaster().then((res) {
      print(res);
      setState(() {
        subjectList = res.subjectList;
        _isLoading = false;
      });
    });
  }

  List<String> headerTitles = [
    "Science",
    "Economics",
    "Accounts",
    "Mathematic",
    "Economics",
    "Accounts",
    "Mathematic",
    "Economics",
    "Accounts"
  ];

  List<String> headerIcons = [
    'assets/Science.svg',
    'assets/Economics.svg',
    'assets/Economics_1.svg',
    'assets/Mathematic.svg',
    'assets/Economics.svg',
    'assets/Economics_1.svg',
    'assets/Mathematic.svg',
    'assets/Economics.svg',
    'assets/Economics_1.svg',
  ];

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: _appBarView(),
        body: _isLoading ? CommonLoading().loadingWidget() : _bodyView());
     
  }

  _appBarView() {
    return PreferredSize(
      child: CustomAppBar(
        titleText: 'Subject List',
        firstIcons: "search",
        bottom: false,
      ),
      preferredSize: Size(MediaQuery.of(context).size.width,
          MediaQuery.of(context).size.height / 10.5),
    );
  }

  _bodyView() {
    return new Container(
      padding: EdgeInsets.only(top: 30.0, left: 20.0, right: 20.0),
      child: new GridView.count(
          crossAxisCount: 3,
          children: List.generate(subjectList.length, (index) {
              return new Center(
    child:
    Container(
      decoration: BoxDecoration(
          border: Border.all(
              width: 0.5,
              // style: BorderStyle.solid,
              color: Theme.of(context).textSelectionColor),
          borderRadius: BorderRadius.all(Radius.circular(5.0))),
      width: MediaQuery.of(context).size.width / 3.8,
      height: MediaQuery.of(context).size.height / 5,
      child: new Column(
        children: <Widget>[
          new Container(
            height: 30.0,
            color: Theme.of(context).highlightColor,
            child: new Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new Text(
                 'Subject List',
                  style: TextStyle(
                      color: Theme.of(context).textSelectionColor,
                      fontSize: 15.0,
                      fontWeight: FontWeight.w600),
                )
              ],
            ),
          ),
          new Container(
              child: new Expanded(
            child: IconButton(
              icon: ThunderSvgIcons(
                path: headerIcons[index],
                height: 60.0,
                color: Theme.of(context).primaryColor,
              ),
              iconSize: 60.0,
              onPressed: () => {}
            ),
          )),
        ],
      ),
    ),
  );
          })),
    );
  }

最佳答案

你正在使用 List.generate(subjectList.length, (index) {...});为您的 GridView 创建小部件(子)列表。因此,索引的最大值等于(subjectList.length - 1)。

然后你在这里使用索引获取headerIcons:

path: headerIcons[index] 

headerIcons 有 9 个项目(headerIcons 数组的最大索引为 8),但 subjectList 中的索引可能大于 8。在这种情况下,您会遇到异常。

热修复:

path: headerIcons[index % headerIcons.length],

关于flutter - RangeError(索引): Invalid value: Not in range 0. .8,包括 : 9 in Gridview. 计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57000148/

相关文章:

ios - Flutter 应用程序停留在物理 iPhone 的安装和启动步骤

flutter - 类型 'Null' 不是类型转换中类型 'Map<String, dynamic>' 的子类型

Flutter 3D 立方体效果

dart - 如何居中对齐堆叠元素,以及如何水平拉伸(stretch)一个?

flutter - 如何在 onesignal 通知上打开特定页面单击 flutter?

dart - Flutter 中的路由守卫

flutter - 为什么在 dart 中赋值运算符后使用 const 关键字?

flutter - 获取flutter中所有可用语言的列表

flutter - 如何在 flutter 中检索 exif 方向值

flutter - NotifierProvider 每次更新都会重新构建并且值不更新