Flutter RangeError(索引):无效值:有效值范围为空:0,颤振

Flutter RangeError(索引):无效值:有效值范围为空:0,颤振,flutter,dart,Flutter,Dart,我试图创建一个食品杂货清单创造者在颤栗。我制作了一个名为GroceryList的类,它以标题、提醒日、配料列表和配料数量列表为参数。我目前正在编写“Finish”按钮,它将获取所有列表信息并将其放入杂货店列表类中。以下是相关代码: final curTitle = new TextEditingController(); int countings = 0; List<String> finalIngs = []; List<String> numfina

我试图创建一个食品杂货清单创造者在颤栗。我制作了一个名为GroceryList的类,它以标题、提醒日、配料列表和配料数量列表为参数。我目前正在编写“Finish”按钮,它将获取所有列表信息并将其放入杂货店列表类中。以下是相关代码:

  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=新文本编辑控制器();
整数计数=0;
列出最终结果=[];
列表numfinalIngs=[];
字符串_memberday=“Sunday”;
小部件构建(构建上下文){
debugPrint(curTitle.text);
返回脚手架(
appBar:appBar(
标题:文本(“新杂货清单”),
领先:IconButton(
图标:常量图标(Icons.arrow\u back\u ios,颜色:Colors.white),
已按下:(){
导航器。推(
上下文
MaterialPage路由(生成器:(上下文)=>ListsPage()),
);
},
),
行动:[
图标按钮(
图标:新图标(Icons.check,颜色:Colors.white),
已按下:(){
如果(最终结果[0]),则不为空&&
numfinalIngs[0]。isNotEmpty&&
curTitle.text.isNotEmpty){
对于(int i=0;i<\u newListIngs.length;i++){
添加(_newListIngs[i].text);
调试打印(最终结果[i]);
numfinalIngs.add(_newlistnumIngs[i].text);
debugPrint(numfinalIngs[i]);
}
杂货店;
cur.title=curTitle.text;
当前提醒日=\u提醒日;
对于(int i=0;i

这门课还有很多内容,但为了整洁,我去掉了它。我得到了上面显示的错误,我知道它的意思和原因,但我不知道该如何修复它。我的意思是,我不知道该怎么做才能永久解决,而不是去掉这个错误。如果你需要更多信息,请告诉我.

我有一个猜测,但我不能确定。我看到了
final curTitle=new textededitingcontroller();
,我看到了这个
grocerylistcur;cur.title=curTitle.text;

但是我看不到TextEditingController的TextField文本,也看不到curTitle的任何标题。我想它是空的。我希望我能帮上忙。如果我的答案是错误的,我很抱歉,因为我是新来的:)祝你过得愉快,开发者

我有一个猜测,但我不能肯定。我看到
final curTitle=new TextEditingController()我看到这个被分配了
grocerylistcur;cur.title=curTitle.text

但我看不到TextEditingController的TextField文本,也看不到curTitle的任何标题,我认为它是空的。我希望我能帮忙。抱歉,如果我的回答是错误的,因为我是新来的:)祝您愉快,开发者

您有很多列表,所以我不确定哪一个列表有问题,但您是否在尝试获取其中的内容之前检查了列表是否为空

if(finalIngs.isNotEmpty && numfinalIngs.isNotEmpty && 
      (finalIngs[0]?.isNotEmpty ?? false) && //What if the Strings are null?
      (numfinalIngs[0]?.isNotEmpty ?? false) && //What if the Strings are null?
      curTitle.text.isNotEmpty)
  ....

显然,如果列表为空,则其他条件必须为false,但它们不会运行,因为不需要它(当使用double&时,
if
发现第一个false时停止)所以它不会显示错误

您有很多列表,所以我不确定哪一个有问题,但您是否在尝试获取其中的内容之前检查过它们是否为空

if(finalIngs.isNotEmpty && numfinalIngs.isNotEmpty && 
      (finalIngs[0]?.isNotEmpty ?? false) && //What if the Strings are null?
      (numfinalIngs[0]?.isNotEmpty ?? false) && //What if the Strings are null?
      curTitle.text.isNotEmpty)
  ....

显然,如果列表为空,则其他条件必须为false,但它们不会运行,因为不需要它(当使用double&时,当发现第一个false时,
if
会停止),因此不会显示错误

我确实有一个textField,但我想我已经去掉了它以清除帖子。谢谢你的帮助。我确实有一个文本字段,但我想我把它去掉是为了清理帖子。谢谢你的帮助,这…似乎奏效了。我不再犯错误了,对此,我很感激。我的提醒似乎暂时不起作用,所以我正在努力解决这个问题,看看我是否已经完成了项目的这一部分。非常感谢!是的,那个。我安装了一个插件,使整个事情更容易,所以现在这对我来说是可行的。如果你想知道的话,我记得它被称为rflutter_Alerth…semes一直在工作。我不再犯错误了,对此,我很感激。我的提醒似乎暂时不起作用,所以我正在努力解决这个问题,看看我是否已经完成了项目的这一部分。非常感谢!是的,那个。我安装了一个插件,使整个事情更容易,所以现在这对我来说是可行的。如果你想知道的话,我记得它叫rflutter\U警报