Flutter: How can I pass data back to Navigator when I use iOS swipe back gesture?

Viewed 275

How can I pass some data back to Navigator when I close the page with iOS swipe back gesture? If I close the page manually, obviously everything works correctly, because when I close it, I call the Navigator.pop() method and pass the data in the arguments. But how do I do this when I go back with swipe gesture?

P.S. WillPopScope will not help, only relevant for Android.

1 Answers

I don't think there's a way to directly pass the result without using Navigator.pop(). I can think of two ways for solving the issue:

  1. If swiping back means "Cancel" (as opposite to e.g. submitting data via some explicit button, where you can call Navigator.pop(result)), then you can distinguish these 2 cases by checking if result of Navigator.push() is not null.
  2. You can use a shared state somewhere up the tree to store data using one of the state management approaches.
Related