flutter - RangeError(index):无效值:有效值范围为空:0,Flutter
问题描述
我正在尝试在 Flutter 中创建一个购物清单创建器。我做了一个类,GroceryList,它以标题、提醒日、成分列表和成分数量列表作为参数。我目前正在编写“完成”按钮,它将获取所有列表信息并将其放入杂货清单类中。以下是相关代码:
final curTitle = new TextEditingController();
int countings = 0;
List<String> finalIngs = [];
List<String> numfinalIngs = [];
String _reminderDay = "Sunday";
Widget build(BuildContext context) {
debugPrint(curTitle.text);
return Scaffold(
appBar: AppBar(
title: Text("New Grocery List"),
leading: IconButton(
icon: const Icon(Icons.arrow_back_ios, color: Colors.white),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ListsPage()),
);
},
),
actions: <Widget>[
IconButton(
icon: new Icon(Icons.check, color: Colors.white),
onPressed: () {
if (finalIngs[0].isNotEmpty &&
numfinalIngs[0].isNotEmpty &&
curTitle.text.isNotEmpty) {
for (int i = 0; i < _newListIngs.length; i++) {
finalIngs.add(_newListIngs[i].text);
debugPrint(finalIngs[i]);
numfinalIngs.add(_newlistnumIngs[i].text);
debugPrint(numfinalIngs[i]);
}
GroceryList cur;
cur.title = curTitle.text;
cur._reminderDay = _reminderDay;
for (int i = 0; i < finalIngs.length; i++) {
cur.ingredients.add(finalIngs[i]);
cur.numIngs.add(numfinalIngs[i]);
}
} else {
showIngAlert(BuildContext context) {
Widget okButton = FlatButton(
child: Text("OK"),
onPressed: () {},
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
content: Text(
"Please fill all blank spaces, and add as needed."),
actions: [
okButton,
],
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
showIngAlert(context);
}
_newListIngs.clear();
_newlistnumIngs.clear();
},
)
],
),
}
这门课还有很多东西,但为了整洁,我把它去掉了。我收到上面显示的错误,我知道这意味着什么以及为什么会出现,但我不知道应该如何解决它。我的意思是,我不知道该怎么做才能获得永久解决方案,而不是摆脱这个错误。如果您需要更多信息,请告诉我。
解决方案
我有一个猜测,但我不能确定。我看到final curTitle = new TextEditingController();
了,我看到了这个分配
GroceryList cur; cur.title = curTitle.text;
但我看不到 TextEditingController 的 TextField 文本或 curTitle 的任何标题,我认为它是空的。我希望我能帮上忙。对不起,如果我的回答是错误的,因为我是新手 :) 祝你有美好的日子,开发者!
推荐阅读
- python - 锁定多线程 pywinauto 发送密钥
- jquery - 如果您只做一个小的滚动轻推或冲动,则滚动到元素
- c# - VS 2017 .Net core 2.2 中缺少 Blazor 模板
- c++ - 创建对象的第二个实例会改变整个类的行为 (C++)
- tomcat - 我的 Tomcat RewriteRules 没有被正确读取
- networking - 用户的 IP 地址可以在浏览会话中使用吗?
- r - 与 R 中的“ifelse”相比,创建新变量的更好方法是什么?
- java - 有没有办法检查某些生成的代码在不使用 implements 关键字时是否遵守接口?
- docker - Docker 容器作为默认应用程序
- mongodb - 在集合中的两个数据之间查找两个日期