【问题标题】:RangeError (index): Invalid value: Not in range 0..8, inclusive: 9 in Gridview.CountRangeError(索引):无效值:不在 0..8 范围内,包括:Gridview.Count 中的 9
【发布时间】:2019-07-12 04:23:21
【问题描述】:

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

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

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

【问题讨论】:

  • 如果我没记错的话,您正在尝试生成一个包含 subjectList.length 项目的列表。您正在尝试访问我认为与 subjectList 具有不同长度的 headerIcons[index]。我认为这就是导致错误出现的原因

标签: flutter dart


【解决方案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],

【讨论】:

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