简体   繁体   English

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

[英]RangeError (index): Invalid value: Valid value range is empty: 0, Flutter

I am trying to create a grocery list creator in Flutter.我正在尝试在 Flutter 中创建一个杂货清单创建者。 I made a class, GroceryList, that takes the title, the reminder day, the list of ingredients, and the list of the number of ingredients as parameters.我做了一个class,GroceryList,它以标题、提醒日、配料表和配料数量表为参数。 I am currently coding the "Finish" button, which will take all of the lists information and put it in a grocery list class.我目前正在编写“完成”按钮,它将获取所有列表信息并将其放入购物清单 class。 Here is the relevant code:以下是相关代码:

  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();
              },
            )
          ],
        ),
  }

There's a lot more to the class, but I got rid of it for the sake of neatness. class 还有很多,但为了整洁,我把它去掉了。 I am getting the error shown above, and I know what it means and why it's there, but I don't know how I should fix it.我收到上面显示的错误,我知道这意味着什么以及为什么会出现,但我不知道应该如何解决它。 I mean, I don't know what to do for a permanent solution, rather than getting rid of this error.我的意思是,我不知道该怎么做才能获得永久解决方案,而不是摆脱这个错误。 Please let me know if you need any more information.如果您需要更多信息,请告诉我。

I have a guess but i can't sure.我有一个猜测,但我不能确定。 I see final curTitle = new TextEditingController();我看到final curTitle = new TextEditingController(); and I see this assigned GroceryList cur; cur.title = curTitle.text;我看到了这个分配的GroceryList cur; cur.title = curTitle.text; GroceryList cur; cur.title = curTitle.text; But i can't see TextField text for TextEditingController or any title for curTitle i think it is null.但我看不到 TextEditingController 的 TextField 文本或 curTitle 的任何标题,我认为它是 null。 I hope i could help.我希望我能帮上忙。 Sorry if my answer is mistake because i am new:) Have a good days for you,developer!对不起,如果我的回答是错误的,因为我是新人:) 祝你有美好的日子,开发者!

You have a lot of lists so I am not sure which one has the problem but have you tried checking if they're empty before trying to get what its inside你有很多列表,所以我不确定哪一个有问题,但你有没有试过检查它们是否是空的,然后再尝试获取里面的内容

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)
  ....

Obviously if the lists are empty the other conditions have to be false, but they won't run because it's not needed (the if stops when it finds the first false when using double &) so it won't show an error显然,如果列表为空,则其他条件必须为假,但它们不会运行,因为它不是必需的( if在使用 double & 时发现第一个假时停止),因此不会显示错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter:RangeError(索引):无效值:有效值范围为空:-1 - Flutter: RangeError (index): Invalid value: Valid value range is empty: -1 flutter error : RangeError (index): Invalid value: 有效值范围为空:0 - flutter error : RangeError (index): Invalid value: Valid value range is empty: 0 RangeError(索引):无效值:有效值范围为空:0 | Flutter Firebase - RangeError (index): Invalid value: Valid value range is empty: 0 | Flutter Firebase RangeError(索引):无效值:有效值范围为空:0 错误 Flutter - RangeError (index): Invalid value: Valid value range is empty: 0 Error Flutter Flutter:RangeError(索引):无效值:有效值范围为空:0 - Flutter: RangeError (index): Invalid value: Valid value range is empty: 0 Flutter &quot;RangeError (index): Invalid value: Valid value range is empty: 0 with WidgetSpan - Flutter "RangeError (index): Invalid value: Valid value range is empty: 0 with WidgetSpan RangeError (index): Invalid value: Valid value range is empty: 0 in flutter Firestore - RangeError (index): Invalid value: Valid value range is empty: 0 in flutter Firestore Flutter RangeError(索引):无效值:有效值范围为空:0 - Flutter RangeError (index): Invalid value: Valid value range is empty: 0 flutter:RangeError(索引):无效值:有效值范围为空: - flutter : RangeError (index): Invalid value: Valid value range is empty: "RangeError (index): Invalid value: Valid value range is empty: 2" with flutter carousel - "RangeError (index): Invalid value: Valid value range is empty: 2" with flutter carousel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM