gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 07:17:10 27 4
gpt4 key购买 nike

我找到了解决方案 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com