Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to get value from second screen while coming back to first screen in flutter

I have two screens

in first screen i have a iconbutton to move to second screen and in second screen i am pop the screen with iconbutton and passing a value as an argument in pop statement..

now i want this value in first screen….

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

its showing Future how to convert in int or any type…so to use it

FIRST SCREEN

IconButton(
              onPressed: () {
                final result = Navigator.of(context)
                    .push(MaterialPageRoute(builder: (context) {
                  return SecondScreen();
                }));

                //here i want to ADD 100 + what i am getting from second screen

                print(result + 100);
              },
              icon: Icon(Icons.arrow_forward_ios))

SECOND SCREEN

IconButton(onPressed: (){
          Navigator.of(context).pop(39);

        },icon: Icon(Icons.arrow_back_ios_new),),

>Solution :

First screen:

IconButton(
  onPressed: () async {
    final valueFromSecondScreen = await Navigator.of(context).push<int>(
      MaterialPageRoute(builder: (context) {
        return SecondScreen();
      }),
    );

    if (valueFromSecondScreen != null) {
      int result = valueFromSecondScreen + 100;
      print(result);
    }
  },
  icon: Icon(Icons.arrow_forward_ios),
)

Second Screen:

IconButton(
  onPressed: () {
    Navigator.of(context).pop(39);
  },
  icon: Icon(Icons.arrow_back_ios_new),
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading