How to startActivityForResult in Flutter?

Most Android apps make use of startActivityForResult at one point or another. But, how do you do this in Flutter?

The official doc

The very helpful Flutter For Android Developers page on the official Flutter website demonstrates a way to do it using Navigator.of(context).pushNamed.

However, in the official doc about Navigation, using MaterialPageRoute is recommended for Material Design apps. So, how do you combine the two?

Adapting the official doc to using MaterialPageRoute

My first attempt provided a crash, as below.

Turned out it was due to using new MaterialPageRoute<Null>, which I had copied from the Navigation page. The solution: replace Null with dynamic.

Code tutorial

For this code tutorial, we will create an app with 2 screens. Tapping a button on the first screen will open the second screen. The second screen has 2 buttons. When tapping either of them, the screen is closed, and the first screen  displays the text of the button that was tapped.

App setup

To follow the code tutorial, create a new app as follows.

If you’re unsure how to set up a Flutter app, check out Getting started with Flutter official tutorial.

Firstly, let’s set up main.dart to launch a MaterialApp.

Secondly, we need to add HomePage to be able to  build the project. We create a new folder home and, in it, home_page.dart.

For HomePage, we use a stateful widget, with the variable _selection tracking the selection made in the other screen. If that variable is null, the screen shows only one button. Otherwise, it shows its value below the button. When tapping the button in HomePage, the method _buttonTapped() is fired (this is where we will launch the second screen from).

Thirdly, we add the second screen, SelectionPage, as a stateless widget. Let’s add a folder named selection and, in it, create selection_page.dart. It contains 2 buttons, which call _selectItem when tapped (this is where we will close this screen and pass on the value back to HomePage).

 

Start Activity For Result implementation

We can now implement the Flutter equivalent of Android SDK’s startActivityForResult.

The code to launch SelectionPage when tapping button on HomePage is as below.

This is the equivalent of Android SDK’s startActivity.

Now, let’s implement closing SelectionPage screen and passing some data when tapping the button in SelectionPage.

And let’s read the data in HomePage (as you would do in Android SDK’s onActivityResult ). Note that instead of using a callback system as in Android SDK, we read the data sent back by SelectionPage as a Future of the code launching SelectionPage.

Voila!

Start Activity For Results screencast
Start Activity For Results screencast

Enjoying this code tutorial? Support the blog!

Options
 

What next?

Bookmark Flutter for Android Developers as it is full of great tricks that will save you time. Then, if you haven’t done so already, read How to write an integration test in Flutter and write a UI test for this code tutorial.

 

Subscribe to my mailing list

Receive a monthly email with a curated list of the best 5 articles on software development I have come across during the month, as well as relevant blog updates such as new posts. The email is sent near the start of each month.
* indicates required

Android app developer with 8 years experience, ranging from start ups to large tech (Google) and non tech (British Airways) companies. Currently freelancing and learning Flutter. LinkedIn natalie ( at ) cogitas (dot) net

Author: Natalie Masse Hooper

Android app developer with 8 years experience, ranging from start ups to large tech (Google) and non tech (British Airways) companies. Currently freelancing and learning Flutter. LinkedIn natalie ( at ) cogitas (dot) net

Leave a Reply

Your email address will not be published. Required fields are marked *