0

In this app there are two screens, the first screen displays all the user input Strings in a List, and the second screen has a TextField which takes the user input which will return to the first screen using onEditingComplete. The user will be able to navigate to the second screen by using a FAB in the first screen. When the user does not type anything in the TextField and returns to the previous page, the String which stores the user input value returns "null". I have added an if statement which will only return to the first page if the TextField is not empty but when i click on submit, it does not navigate to the first screen at all.

Could i get a suggestion on how i can modify the code so that null value from the TextField will not save to the String and navigate back to the first screen.

FAB which navigates to input(second) screen:

FloatingActionButton(
            onPressed: ()async {
//                setstring(String result){
//                  widget.updatestring = result;
//                  return widget.updatestring;
//                }
                String result1 =   await Navigator.push( // string which stores the user entered value
                  context,
                  MaterialPageRoute(
                    builder: (context) => InputScreen(), //screen which has TextField
                  ));
              setState(() {
//                widget.updatestring = result1;
                TodoList(result1);
//              setstring(result1);
                addItem(result1, false); // function which adds the entered task in a list
              });
            },
            heroTag: "btn2",
            child: Icon(Icons.add, color: Color(whitecolor),), backgroundColor: Color(redcolor),),

Textfield which takes user input :

                     TextField(
                        autofocus: true,
                          onEditingComplete: (){
                            String textToSendBack = taskcontroller.text;
                            if(taskcontroller.text.isNotEmpty) {
                              Navigator.pop(context, textToSendBack);
                            }
                            },
//                        onSubmitted: (value) {
//                          String textToSendBack = taskcontroller.text;
//                          Navigator.pop(context, value);
//                          },
                        maxLength: 100,
                        controller: taskcontroller,
                        decoration: InputDecoration(
                          labelText: "enter tasks here"
                        ),
                        style: TextStyle(height: 1.2, fontSize: 20, color: Colors.black87),
                      )

2 Answers 2

3

For me it's working fine isEmpty or isNotEmpty

_titleController.text.isEmpty
_titleController.text.isNotEmpty
Sign up to request clarification or add additional context in comments.

Comments

0

you can do something like this:

String textToSendBack = taskcontroller.text;
       if(textToSendBack != null){
              Navigator.pop(context, textToSendBack);
          }

that will only take you back to the other screen when the Text isn't empty. otherwise, you stay on the current screen waiting for valid input.

I hope I understood you question correctly.

2 Comments

it navigates back to the first screen but the String is still saved with blank space. I want it to navigate to the first screen without saving anything in the String textToSendBack.
ok i solved it by adding the same condition (String.isNotEmpty) to the function which adds the tasks and now it does not save to the string. Thank you for helping out :).