If you have worked on Android applications before then you must be aware that there is a function as startActivityForResult, which starts an activity and that activity returns a result to the previous activity.
Check more about our Flutter app development.
In this blog, we will see how we can achieve this in Flutter
- Push the new widget as the below-provided code.
1await Navigator.pushNamed(context, secondWidgetRoute);
Here in the above code, you can see that we have pushed the new widget with an await prefix. Using this our function will wait till it gets some result from the next widget. - Will doing the pop from the second widget add the data as the second parameter
1Navigator.pop(context, result);
As we can see, we have added the result (which could be anything) to the pop function and the first widget can receive this response. - To receive the result, you need to assign the await function to a variable.
1var results = await Navigator.pushNamed(context, secondWidgetRoute);
and after this, you can get the results by casting this var into the required data.
123if (results != null) {// you code goes here}
Remember to keep the await function inside an async function. It will only in this condition.
1 2 3 4 5 6 |
_startSecondWidget() async { var results = await Navigator.pushNamed(context, secondWidgeteRoute); if (results != null) { // You code goes here. } } |
You can easily achieve the functionality of startActivityForResult just like the native Android.
That’s all for this blog. Thank you very much. This is Vedesh Kumar signing off.