Return data from a screen | data return
Cookbook[1]Navigation[2]Returndatafromascreen[3]Insomecases,youmightwanttoreturndatafromanewscreen.Forexample,sayyoupushanewscreenthatpresentstwooptionstoauser.Whentheusertapsanoption,youwanttoinformthefirstscreenoftheuser’sselectionsothatitcanactonthatinformation.YoucandothiswiththeNavigator.pop()[4]methodusingthefollowingsteps:DefinethehomescreenAddabuttonthatlaunchestheselectionscreenShowtheselectionscreenwithtwobuttonsWhenabuttonistapped,closetheselectionscreenShowasnackbaronthehomescree...
Cookbook [1] Navigation [2] Return data from a screen [3]In some cases, you might want to return data from a new screen. For example, say you push a new screen that presents two options to a user. When the user taps an option, you want to inform the first screen of the user’s selection so that it can act on that information.
You can do this with the Navigator.pop()[4] method using the following steps:
Define the home screen Add a button that launches the selection screen Show the selection screen with two buttons When a button is tapped, close the selection screen Show a snackbar on the home screen with the selection 1. Define the home screenThe home screen displays a button. When tapped, it launches the selection screen.
class HomeScreen extends StatelessWidget { const HomeScreen({super.key}); @override Widget build(BuildContext context) { retu...