Data pass between Flutter and Android using the Method channel

tanya anand
3 min readMay 10, 2022

To create a real-world App, it's important to pass data to and fro between flutter and the host Android App.

Method channel is a way for data passing. Now, let's have a look at how can we do this, with some code examples.

First and foremost, to use the method channel we have to go with the cached engine implementation method.

Let's do things one by one,

Step 1:

Create a method channel for data passing along with cached flutter engine

We have created a flutter engine.

setInitialRoute helped in setting the initial route for the flutter engine.

executeDartEntrypoint( DartExecutor.DartEntrypoint.createDefault() ) helped in executing dart code to pre-warm the flutter engine, so that there wont be any delay while launching a flutter activity.

FlutterEngineCache .getInstance() gives an instance of cache where we can store our newly created flutter engine.

MethodChannel( cachedFlutterEngine?.dartExecutor, channelId ) return a method channel, which helps in data passing. every method channel will have a different channelId. which helps in identification. we will understand its use further.

Step 2:

Host android app implementation

this will create a method channel instance, which will be used further.

To send data from Android host App to Flutter.

AppConstants.DATA_PASS_METHOD is defined to identify the method name over which the data is passed. this will be used on the flutter end to parse data.

We can use the same method-channel to receive data from the Flutter module and send some acknowledgment to flutter.

FlutterCallbackMethodNames.TRIGGER_ANALYTICS_EVENT and FlutterCallbackMethodNames.GET_ACCESS_TOKEN are method names over which data is sent from the flutter end and is mapped on the android side to receive respective data and process it.

In second method FlutterCallbackMethodNames.GET_ACCESS_TOKEN we are also using result.success(AuthUtils.getAuthentication()?.access_token). This is used to send acknowledgment data back to the flutter module. we are using it for refreshing our access token on every API call.

Now, you can start your flutterActivity

That’s all on Android side!!

Step 3:

Flutter module implementation

AppConstants.FLUTTER_CHANNEL_ID needs to be the same on both the host and flutter end, to identify the method channel.

Also, AppConstants.DATA_PASS_METHOD should be the same to identify the method name and parse the actual data.

Now, let's see how we can send data from flutter to the android host app.

Inside class AnalyticsHelper we have sent data to android and forget what happened there, same as fire and forget rule.

But inside tokenRegeneration, we waited for the acknowledgment from the android host side.

Note: Always make sure the names you are using for mapping is always same on flutter and android side.

That’s all about Flutter changes.

Hope! This helps you somewhere in your journey.

Please share some love by putting in any comment if you find something, not in your favor. 🧚🏼

--

--