6

Navigator.pop method can return only one value but I need to return multiple values. Is there any way to return multiple values using Navigator.pop()?

I've previously used Navigator.push but when I push multiple variables from many pages to one page it shows construction error.

I'have been stuck here for days.

Please help me.

Thanks in advance.

magicleon94
  • 4,887
  • 2
  • 24
  • 53
shadid
  • 97
  • 1
  • 3
  • Why do you want to return multiple values using ```Navigator.pop()```? What logic are you trying to enact? – tomerpacific Aug 06 '19 at 07:14
  • i just want to get data from different page to home where i use those data as input to api. i want to do what momondo app does form flight search. – shadid Aug 06 '19 at 10:05

6 Answers6

6

You can pop in two ways:

Navigator.of(context).pop(object);

or

Navigator.pop(context, object);

Either way, I've flagged with object the optional return value for the method.

If you desire to return more than one values, you'll need to box them in an object, class or Map or whatever.

Boxing with a class will look like this:

class BoxedReturns{
    final int a;
    final int b;

    BoxedReturns(this.a, this.b);
}

//stuff
Navigator.of(context).pop(BoxedReturns(1,2));

You can do something similar by using a map, although I'd rather use the class approach:

Navigator.of(context).pop({"a":1,"b":2});
magicleon94
  • 4,887
  • 2
  • 24
  • 53
2

You can use Map

final data = { "key1" : "value1", "key2" : "value2" };
Navigator.of(context).pop(data);
Bhargav Sejpal
  • 1,352
  • 16
  • 23
1

To be able to return multiple values via Navigator.pop() you could do 2 things (surely even more ways but those are some basic ones):

1. Creating a model holding those data:

class YourClass {
  String firstVar;
  String secondVar;
  int thirdVar;

  News(
      {this.firstVar,
      this.secondVar,
      this.thirdVar});
}

Inside your view where you want to return data with Navigator.pop():

...
Navigator.pop(context, YourClass('test', 'test2', '1'));

2. Using a Map to hold nested (in my case tupels):

...
Map<String, int> myData = new Map();
myData['test'] = 1;
myData['test2'] = 2;
Navigator.pop(context, myData);
kounex
  • 1,555
  • 4
  • 12
0

Make a new data structure class containing all data.
You can also use map or list, but it will be a possible bug source if data structure changes.

Mikhail Ponkin
  • 2,563
  • 2
  • 19
  • 19
0

Similar question Flutter Back button with return data.
code snippet from Deepak Thakur

class DetailsClassWhichYouWantToPop {
  final String date;
  final String amount;
  DetailsClassWhichYouWantToPop(this.date, this.amount);
}

void getDataAndPop() {
      DetailsClassWhichYouWantToPop detailsClass = new DetailsClassWhichYouWantToPop(dateController.text, amountController.text);
      Navigator.pop(context, detailsClass); //pop happens here
  }

new RaisedButton(
    child: new Text("Edit"),
    color:  UIData.col_button_orange,
    textColor: Colors.white,
    onPressed: getDataAndPop, //calling pop here
  ),
chunhunghan
  • 51,087
  • 5
  • 102
  • 120
  • How do we access the result value? I get error like `Error: The getter 'amount' isn't defined for the class 'Object'.` and the program doesn't compile. – skadoosh Jun 25 '20 at 08:20
  • @skadoosh, Need to reproduce your question. could you put reproduce code to a new question and ping my name. thanks. – chunhunghan Jun 29 '20 at 00:42
0

Simple Example:

enter image description here

You can send your custom object via result parameter.

Push to and get result from new Screen: Navigate to new-screen wait for then() or complete() event to happen.

pushToNewScreen() {
  Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => NewScreen()),
  ).then((value) {
    // if value is true you get the result as bool else no result 
    if (value != null && value == true) {
      print('Do something after getting result');
    } else {
      print('Do nothing');
    }
  });
}

Get Data from new Screen when some event happens

ontap: (){
    // pass your custom object inlace of bool value
    Navigator.pop(context, true);
}
jazzbpn
  • 6,441
  • 16
  • 63
  • 99