Search code examples
flutterdartback-button

Flutter Back button with return data


I have an interface with two buttons that pop and return true or false, like so:

onPressed: () => Navigator.pop(context, false)

I need to adapt the back button in the appbar, so it pops and also returns false. Is there a way to accomplish this?


Solution

  • This may help and work for you

    1st screen

    void goToSecondScreen()async {
     var result = await Navigator.push(_context, new MaterialPageRoute(
     builder: (BuildContext context) => new SecondScreen(context),
     fullscreenDialog: true,)
    );
    
    Scaffold.of(_context).showSnackBar(SnackBar(content: Text("$result"),duration: Duration(seconds: 3),));
    }
    

    2nd screen

    Navigator.pop(context, "Hello world");