Flutter Android Toast Message Using platform channel

Ishan Fernando
CodeChai
Published in
2 min readMar 9, 2019

Android toast notification can use to give a quick message to the user and It will disappear after a few seconds.

But when It comes to the Flutter there is no straightforward way to show these toast messages. Therefore, we need to find an alternative approach to implement that. In that case, the platform channel will be your friend.

What is Platform channel

Platform channel will allow developers to execute platform specific code through the flutter app. Actually, it’s a message passing style. It has mainly 2 steps

  • Flutter application will send his message to the Android or ios (host) portion of the app.
  • The host will execute the platform specific task and return the response to the client.

Because of the platform channel, we can feel the true power of android and ios inside the flutter app.

Implementation

First, let’s implement dart code and then we can move to the Android Side.

Flutter side

We need MethodChannel to create a channel in the Flutter. First, we need to import the flutter service package.

import 'package:flutter/services.dart';

Next, we need to create an object of MethodChannel class

static const platform = const MethodChannel("toast.flutter.io/toast");

In here we need to pass a unique channel name to the constructor. This channel name used to identify each channel uniquely.

Next, we need to specify which method needs to invoke In Android Side. You can use any method name here. We can call this method when someone clicks a button.

platform.invokeMethod("showToast");

Android side

Next move to the native Android side. For that, we need to open the MainActivity file. In your editor, you can follow this path to open MainActivity file.

android/src/main/java/<packageName>/MainActivity

First, you can create a variable to assign the channel name. In here we need to use the same channel name which we were specified in the flutter code.

private static final String CHANNEL = "toast.flutter.io/toast";

In here we need to create a new MethodChannel Object. As the second parameter of the constructor, we need to pass the channel name. The method call handler will invoke when we call the method from flutter side using invokeMethod. Inside the onMethodCall, we need to check which method. Because we can call multiple methods through the single platform channel.

new MethodChannel(getFlutterView(),CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() { @Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
if(methodCall.method.equals("showToast")){
Toast.makeText(getApplicationContext(),"Flutter Toast",Toast.LENGTH_SHORT).show();
}
else {
result.notImplemented();
}
}
});

Then run the app in the Android simulator and you can see the Toast messages for each click.

If you have any doubt you can get the project from below and check the code.

Project link

https://github.com/IshanFx/flutter-toast

References

https://flutter.dev/docs/development/platform-integration/platform-channels

Originally published at mightytechno.com on March 9, 2019.

--

--

Ishan Fernando
CodeChai

JavaScript | TypeScript | Angular | Flutter | React