【发布时间】:2021-03-27 01:12:21
【问题描述】:
这是我在这里的第一个问题。 (所以如果出现任何问题,请告诉我。)
我是 Flutter 的新手,我试图在我的应用中应用 GridView.count,但出现以下错误。
Unhandled Exception: RangeError (index): Invalid value: Not in inclusive range 0..4: 5
我看到ListView 有类似的问题,从解决方案中,我试图找到类似itemCount 或childCount 的东西,但没有找到类似的东西。
所以我的代码如下。主要的GridView.count 是这样调用的:
body: GridView.count(
crossAxisCount: 2,
children: buttons,
)
和小部件列表,buttons 由getButtonList() 函数设置:
List<Widget> buttons = [];
void getButtonList() async {
List<Map> list = await database.rawQuery('SELECT * FROM test WHERE mom > 2');
for (int i = 0; i < list.length; i++) {
//print(list[i]['name']); //this seems ok
setState(() {
buttons.add(RaisedButton(
onPressed: () {},
child: Text(list[i]['name']),
),
);
});
}
// and later in Floating Action Button callback like this
floatingActionButton: FloatingActionButton(
onPressed: () {
getButtonList();
},
),
我还尝试从getButtonList() 返回一个临时列表,并在浮动操作按钮中用setState 包装,如下所示:
List<Widget> buttons = [];
Future<List<Widget>> getButtonList() async {
List<Widget> temp = [];
List<Map> list = await database.rawQuery('SELECT * FROM test WHERE mom > 2');
for (int i = 0; i < list.length; i++) {
//print(list[i]['name']);
setState(() {
temp.add(
RaisedButton(
onPressed: () {},
child: Text(list[i]['name']),
),
);
});
}
return temp;
}
// and Floating Action Button callback like this
floatingActionButton: FloatingActionButton(
onPressed: () async {
List<Widget> temp = await getButtonList();
setState(() {
buttons = temp;
});
},
),
仍然显示相同的错误消息。
【问题讨论】: