hgl
hgl

Reputation: 2214

How to return data when popping multiple screens?

I know I can return data to the previous screen by using

Navigator.pop(context, 'Value');

But in my case I need to pop multiple screens by using

Navigator.popUntil(context, ModalRoute.withName('/login'));

I wonder in this case how do I pass the data back to the corresponding widget?

Thanks in advance.

Upvotes: 3

Views: 1726

Answers (3)

Kunchok Tashi
Kunchok Tashi

Reputation: 2964

Say you have Screen A,Screen B, Screen C. If you want to pop till Screen A and pass some data. Here is what you have to do.
1. Navigate from Screen A to Screen B

Navigator.pushNamed(context, '/screenb')
              .then((value) {
           //you will get return value here
          });

2. To pop till Screen A.

//add thi code in Screen C
var nav = Navigator.of(context);
nav.pop('refresh');
nav.pop('refresh');

Upvotes: 1

humanshado
humanshado

Reputation: 188

The flutter API does not have that feature and from this https://github.com/flutter/flutter/issues/30112 discussion, that feature is not on the table yet. A walkaround was suggested though using the Page API.

However, in my opinion, it is cleaner to use the provider package https://pub.dev/packages/provider as part of your app state management to keep the data you want and make it available to any screen of interest. Follow these steps to achieve that.

  1. Add the provider to your pubspec.yaml. Check the link above to see detailed instructions.

  2. Create a notifier class that extends ChangeNotifier class as shown below. ChangeNotifier class is part of the flutter API.

class MyDataProvider extends ChangeNotifier {

    //define your private data field(s). I'm using int here.
    int _mydata;

    //define a getter
    int get myData => _myData;

    // define a setter
    set myData(newData){
      _myData = newData;
      notifyListeners();
    } 
}

  1. Wrap your uppermost widget (or the parent of the screens where you want to pass the data) with the provider and instantiate it. I'm using main here.
void main(){
  runApp(
     ChangeNotifierProvider(create: (context) => MyDataProvider()),
   child: MyApp(),
  )
}

  1. Assuming you have five screens: Screen1, Screen2, ..., Screen5 and you want to navigate to screen5, do some operations and return to screen1 with some data. On 1st screen, define a local variable for myData and create an instance of your myDataProvider. When a button is pressed to start the navigation, wrap up the push navigation in an asynchronous call.
//Screen1

int currentLocalData = 78;

MyDataProvider myProvider = Provider.of<MyDataProvider>(context);

onPressed: () async {
  //Assign localData to myData in the provider
  myProvider.myData = currentLocalData; //calls the setter define in the provider.
  await Navigator.push(context, MaterialPageRoute(
       builder: (context) => Screen5()
  ));

 //Retrieve myData from the provider and assign it to currentLocalData. 
//This executes after navigating back from Screen5
  currentLocalData = myProvider.myData;
}
  1. Let assume in Screen5 you retrieved the data and added 100 to it. Your aim is to return to Screen1 and make use of the new data, i.e 178. Here you will instantiate the provider again in Screen5 and assign the value 178 to myData.
//Screen5

MyDataProvider myProvider = Provider.of<MyDataProvider>(context);

myProvider.myData += 100;

//Use navigation.popUntil
Navigation.popUntil(context, ModalRoute.withName('/Screen1'));

Upvotes: 1

Arun R. Prajapati
Arun R. Prajapati

Reputation: 2802

you can send DATA in few ways

  1. as a Parameter
  2. using Shared_Preferences
  3. using Static Variables

Only for Current Session if you just need the DATA for Current Session you can go for Static Variables

step 1 : Create a Class and have Static Variable in it.

    class Globaldata{
     static String value;
    }

step 2 : Initialise variable by

    Globaldata.value="some_value";

step 3 : use of variable

    String assigned_value = Globaldata.value;

Upvotes: 1

Related Questions