Danzon2504
Danzon2504

Reputation: 91

Return null Future from Navigator.pop()

I'm passing values from one page to another as mentioned in the official documentation. https://flutter.dev/docs/cookbook/navigation/returning-data

I implemented two different buttons.

  1. Submit button which returns a list of String.

  2. Close button which returns null.

                 ElevatedButton(
                 onPressed: () async {
                   _formKey.currentState?.save();
                   if (_formKey.currentState!.validate()) {
                     _formKey.currentState?.fields.forEach((key, value) {
                       // return a list with the names
                       widget._names.add(value.value);
                       print(widget._names);
                     });
                     Navigator.pop(context, widget._names);
                   }
                 },
                 child: Text('Speichern & Zurück')),
             OutlinedButton(
                 onPressed: () {
                   Navigator.pop(context, null);
                 },
                 child: Text('Verwerfen & Zurück'))
    

The value is the return value of the Navigator.push() - method.

                            result = Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => PlayerNames(
                                    _currentBoySliderValue,
                                    _currentGirlSliderValue)));

I catch the null Future by simply checking:

                      if (result != null) {
                    // does not catch Future<dynamic> with null content 
                  }

Unfortunately, the Future (result) is only null if the Page PlayerNames() is never called, because pressing the Button which returns null, does not make the Future go null, but its content. This leads to the unintentional behaviour, that if the user inserts no data in the page and uses the button, which results in returning null my methods are called on a future with null values.

Is there any chance to set a Future to null, if the value that stores this information is the target? I don't want to add code for checking if the values of the future are null because it's not always permitted to have null data.

Upvotes: 0

Views: 2675

Answers (3)

Zeeshan Ahmad
Zeeshan Ahmad

Reputation: 676

You should add await while waiting for the response from PlayerNames.

To return data from another screen in Flutter, you have to wait for the screen to finish and return data. await keyword waits for the screen to finish.

For further clarification, you can follow this link

Upvotes: 1

KuKu
KuKu

Reputation: 7492

How about you change return data type to Map?

             ElevatedButton(
             onPressed: () async {
               _formKey.currentState?.save();
               if (_formKey.currentState!.validate()) {
                 _formKey.currentState?.fields.forEach((key, value) {
                   // return a list with the names
                   widget._names.add(value.value);
                   print(widget._names);
                 });
                 Navigator.pop(context, { 'isExistData': true, 'data': widget._names });
               }
             },
             child: Text('Speichern & Zurück')),
         OutlinedButton(
             onPressed: () {
               Navigator.pop(context, { 'isExistData': false });
             },
             child: Text('Verwerfen & Zurück'))
if (result['isExistData']) {
  // Do something
  var data = result['data']

}

Upvotes: 0

pedroafonso
pedroafonso

Reputation: 31

Maybe, instead return null, just return an empty array '[ ]' and then validate if the result is empty.

Navigator.pop(context, []);

      if (result.isEmpty()) {
    // do some stuff
  }

Upvotes: 0

Related Questions