Search code examples
flutterdartflutter-navigation

Flutter Navigation with values between different screens


I would be very grateful if someone could explain how to deal with the following problem:

I have a screen with 2 fields which redirect me to 2 different pages. On each of these pages I have a text field that uses a controller to push the text into the field on the previous page.

However, I don't have a solution for this, because when I work with Navigator.push one field is always empty. If I work with Navigator.pop, I cannot transfer any data (can I?).

Is there any possibility to use Navigator.pop and pass some data or something like that?


Solution

  • You can get results from your second screen with waiting on push

    here is an example:

    you call this in first page

    final result = await Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => SecondPage(),
        )
      );
    

    and In your second screen, when you are calling pop, pass data to it, like this:

    Navigator.pop(context, controller.text);
    

    then in your first page, you have controller text in result variable