RangeError(索引):无效值:不在0..8范围内,包括:Gridview.Count中的9

时间:2019-07-12 04:23:21

标签: flutter dart

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

引发了另一个异常:RangeError(索引):无效值:不在0..8范围内(包括9)

[![在此处输入图片描述] [1]] [1]

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: () => {}
            ),
          )),
        ],
      ),
    ),
  );
          })),
    );
  }```


  [1]: https://i.stack.imgur.com/9OaAi.png

1 个答案:

答案 0 :(得分:1)

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

然后您使用索引在此处获取headerIcons:

path: headerIcons[index] 

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

修补程序:

path: headerIcons[index % headerIcons.length],