【问题标题】:RangeError (index): Invalid value: Valid value range is empty: 0, FlutterRangeError(index):无效值:有效值范围为空:0,Flutter
【发布时间】:2020-11-01 23:16:12
【问题描述】:

我正在尝试在 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();
              },
            )
          ],
        ),
  }

这门课还有很多,但为了整洁,我把它去掉了。我收到上面显示的错误,我知道这意味着什么以及为什么会出现,但我不知道应该如何修复它。我的意思是,我不知道该怎么做才能获得永久解决方案,而不是摆脱这个错误。如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    我有一个猜测,但我不能确定。我看到final curTitle = new TextEditingController();,我看到这个分配 GroceryList cur; cur.title = curTitle.text; 但是我看不到 TextEditingController 的 TextField 文本或 curTitle 的任何标题,我认为它是空的。我希望我能帮上忙。对不起,如果我的回答是错误的,因为我是新手 :) 祝你有美好的日子,开发者!

    【讨论】:

    • 我确实有一个 textField,但我想我摆脱它是为了清理帖子。感谢您提供帮助。
    【解决方案2】:

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

    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,但它们不会运行,因为它不需要(if 在使用 double & 时发现第一个 false 时停止)所以它不会显示一个错误

    【讨论】:

    • 这……似乎奏效了。我不再收到错误,为此,我很感激。不幸的是,我的警报似乎不起作用,所以我正在努力解决这个问题,看看我是否已经完成了项目的这一部分。非常感谢!
    • 是的,那个。我安装了一个插件来使整个事情变得更容易,所以现在这对我有用。如果你想知道,我记得它叫 rflutter_alert
    猜你喜欢
    • 2020-04-19
    • 2020-06-24
    • 2021-10-06
    • 2021-11-13
    • 2022-08-17
    • 1970-01-01
    • 2021-06-16
    • 2021-07-31
    • 1970-01-01
    相关资源
    最近更新 更多