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….

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),
)

Leave a Reply