0

I am trying to create a grocery list creator in 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. I am currently coding the "Finish" button, which will take all of the lists information and put it in a grocery list 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. 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.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

That...semes to have worked. I'm not getting the error anymore, and for that, I am grateful. Unfourtanetly my alert doesn't seem to work, so I'm working to fix that and see if I've completed this part of the project. Thanks so much!
Yeah, that. I installed a plugin to make the whole thing easier, so now that's working for me. If you want to know, I recall it was called rflutter_alert
1

I have a guess but i can't sure. I see final curTitle = new TextEditingController(); and I see this assigned 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. I hope i could help. Sorry if my answer is mistake because i am new :) Have a good days for you,developer!

1 Comment

I do actually have a textField, but I guess I got rid of it to clear up the post. Thanks for trying to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.