我找到了解决方案 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: () => {}
),
)),
],
),
),
);
})),
);
}
最佳答案
你正在使用 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/
我发现自己需要这个。假设cart是一个包含用户列表的模型。defindex_of_itemcart.users.each_with_indexdo|u,i|ifu==current_userreturniendend获取此类关联索引的更简单方法是什么? 最佳答案 indexArray上的方法与您的index_of_item方法相同,例如cart.users.index(current_user)返回数组中第一个对象的索引==给obj。如果未找到匹配项,则返回nil。 关于ruby-on-
因此,当我遵循MichaelHartl的RubyonRails教程时,我注意到在用户表中,我们为:email属性添加了一个唯一索引,以提高find的效率方法,因此它不会逐行搜索。到目前为止,我们一直在根据情况使用find_by_email和find_by_id进行搜索。然而,我们从未为:id属性设置索引。:id是否自动索引,因为它在默认情况下是唯一的并且本质上是顺序的?或者情况并非如此,我应该为:id搜索添加索引吗? 最佳答案 大多数数据库(包括sqlite,这是RoR中的默认数据库)会自动索引主键,对于RailsMigration
我想编写一个ruby脚本来递归复制目录结构,但排除某些文件类型。因此,给定以下目录结构:folder1folder2file1.txtfile2.txtfile3.csfile4.htmlfolder2folder3file4.dll我想复制这个结构,但不包含.txt和.cs文件。因此,生成的目录结构应如下所示:folder1folder2file4.htmlfolder2folder3file4.dll 最佳答案 您可以使用查找模块。这是一个代码片段:require"find"ignored_extensions=[".cs"
假设我有一个可枚举对象enum,现在我想获取第三个项目。我知道一种通用方法是转换成数组,然后使用索引访问,如:enum.to_a[2]但这种方式会创建一个临时数组,效率可能很低。现在我使用:enum.each_with_index{|v,i|breakvifi==2}但这非常丑陋和多余。执行此操作最有效的方法是什么? 最佳答案 你可以使用take剥离前三个元素,然后剥离last从take给你的数组中获取第三个元素:third=enum.take(3).last如果您根本不想生成任何数组,那么也许:#Ifenumisn'tanEnum
我想写一点“Deprecate-It”库并经常使用“method_added”回调。但是现在我注意到在包含模块时不会触发此回调。是否有任何回调或变通方法,以便在某些内容包含到自身时通知类“Foobar”?用于演示的小Demo:#IncludingModulswon'ttriggermethod_addedcallbackmoduleInvisibleMethoddefinvisible"Youwon'tgetacallbackfromme"endendclassFoobardefself.method_added(m)puts"InstanceMethod:'#{m}'addedto'
我有一个正则表达式来获取"*"之间的所有内容:str="Donecsedodiodui.*Nullamiddoloridnibhultriciesvehiculaut*"str.match(/\*(.*)\*/)[1]我希望匹配能够包含换行符。我该怎么做? 最佳答案 您需要使用允许点匹配新行的m选项:Donecsedodiodui.*Nullamiddoloridnibhultriciesvehiculaut*regexstr.match(/\*(.*)\*/m)[1]实例:http://www.rubular.com/r/11u9
在我的场景中,Logstash收到的系统日志行的“时间戳”是UTC,我们在Elasticsearch输出中使用事件“时间戳”:output{elasticsearch{embedded=>falsehost=>localhostport=>9200protocol=>httpcluster=>'elasticsearch'index=>"syslog-%{+YYYY.MM.dd}"}}我的问题是,在UTC午夜,Logstash在外时区(GMT-4=>America/Montreal)结束前将日志发送到不同的索引,并且索引在20小时(晚上8点)之后没有日志,因为“时间戳”是UTC。我们已
我想从特定索引开始遍历数组。我该怎么做?myj.eachdo|temp|...end 最佳答案 执行以下操作:your_array[your_index..-1].eachdo|temp|###end 关于ruby-从特定索引开始迭代数组,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/44151758/
我一直在努力学习如何处理由数组组成的数组。假设我有这个数组:my_array=[['ORANGE',1],['APPLE',2],['PEACH',3]我将如何找到包含'apple'的my_array索引并删除该索引(删除子数组['APPLE',2]因为'apple'包含在该索引的数组中)?谢谢-我非常感谢这里的帮助。 最佳答案 您可以使用Array.select过滤掉项目:>>a=[['ORANGE',1],['APPLE',2],['PEACH',3]]=>[["ORANGE",1],["APPLE",2],["PEACH",3
我想使用部分字符串搜索数组,然后获取找到该字符串的索引。例如:a=["Thisisline1","Wehaveline2here","andfinallyline3","potato"]a.index("potato")#thisreturns3a.index("Wehave")#thisreturnsnil使用a.grep将返回完整的字符串,使用a.any?将返回正确的true/false语句,但都不会返回匹配的索引找到了,或者至少我不知道该怎么做。我正在编写一段代码,该代码读取文件、查找特定header,然后返回该header的索引,以便它可以将其用作future搜索的偏移量。如果