1
votes

I found a solution ListView.builder "You should pass the itemCount parameter to the ListView.builder to allow it to know the item count" but not working for GridView.count.

Another exception was thrown: 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: () => {}
            ),
          )),
        ],
      ),
    ),
  );
          })),
    );
  }
1
If I'm not mistaken, you're trying to generate a list which have subjectList.length items. And you are trying to access the headerIcons[index] which I assumed have a different length with subjectList. I think that's what make the error appear - Bobby Stenly

1 Answers

1
votes

You are using List.generate(subjectList.length, (index) {...}); to create list of widgets (children) for your GridView. Therefore, the max value of index equals (subjectList.length - 1).

Then you use the index to get headerIcons here:

path: headerIcons[index] 

The headerIcons has 9 items (max index of headerIcons array is 8), but the index from subjectList could be larger than 8. In that case you get the exception.

Hot fix:

path: headerIcons[index % headerIcons.length],