'Pass data to the previous screen
I'm building an app which requires data from a page to the previous page. I'm using Navigator.pop(context)
; to exit from the current page at the same time refreshing the previous page with a function. I need to pass an ID to the previous page for an API calling. How can I pass a data by Navigagtor.pop(context)
.
Solution 1:[1]
Try below code: Navigator.pop(context, 'Back!');
ElevatedButton(
onPressed: () {
Navigator.pop(context, 'You are old screen!');
},
child: const Text('Back Screen!'),
),
Solution 2:[2]
From the First Page you can navigate to Second one. The "Navigator.push" returns a Future. Use "then" to get the value returned from Second page.
Navigator.push(context, MaterialPageRoute(builder: (context) => SecondPage())).then((value) {
// Handle 'value' here.
// 'value' is returned by second page through Navigator.pop(context, 'some relevante value or object');
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ravindra S. Patil |
Solution 2 | Michel Fortes |