Event Channels in Flutter allow you to communicate between your Dart code and platform-specific code. It can be in Java or Kotlin for Android or Objective-C or Swift for iOS. Event Channels provide a bi-directional communication channel. Allows you to send messages from Flutter to the platform and receive messages back from the platform.
If you’re looking for a Flutter app development company, we are the perfect choice.
Event Channel working
To set up an Event Channel, you need to create a class in your platform-specific code that implements the FlutterStreamHandler
interface. This interface has three methods: onListen
, onCancel
, and onPause
. The onListen
method is called when the Event Channel is opened, and the onCancel
method is called when the Event Channel is closed. The onPause
method is called when the Event Channel is paused, which happens when the Flutter app is sent to the background.
Once you have your platform-specific class set up, you can create an Event Channel in your Flutter code using the EventChannel
class. The EventChannel
class takes a channel name as an argument, which should match the channel name defined in your platform-specific class. You can then use the receiveBroadcastStream
method of the EventChannel
class to listen for messages from the platform.
To send messages from Flutter to the platform, you can use the EventChannel
class’s invokeMethod
method.
takes a method name and an optional argument, which will be passed to the platform-specific code as a parameter. In your platform-specific class, you can use the invokeMethod
invokeMethod
method of the MethodChannel
class to send messages back to Flutter.
Event Channels can be useful for a variety of purposes. Such as communicating with hardware devices or integrating with platform-specific APIs that are not available in Flutter. However, they can also be more complex to set up and use than other communication methods. So, it’s important to consider whether they are the best solution for your particular use case.
Code implementation
Here’s an example of how to set up an Event Channel in Flutter and send messages between Flutter and Android:
In your Android code, create a class that implements the FlutterStreamHandler
interface:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class MyEventChannel implements FlutterStreamHandler { private EventChannel.EventSink eventSink; @Override public void onListen(Object arguments, EventChannel.EventSink events) { // The Flutter app has opened the Event Channel eventSink = events; } @Override public void onCancel(Object arguments) { // The Flutter app has closed the Event Channel eventSink = null; } public void sendEvent(String message) { // Send a message to the Flutter app if (eventSink != null) { eventSink.success(message); } } } |
In your Flutter code, create an instance of the EventChannel
class and listen for messages:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import 'package:flutter/services.dart'; final myEventChannel = EventChannel('my_event_channel'); StreamSubscription subscription; void setupEventChannel() { subscription = myEventChannel.receiveBroadcastStream().listen((message) { // Handle incoming message print('Received message: $message'); }); } void closeEventChannel() { subscription.cancel(); } |
To send a message from Flutter to Android, call the invokeMethod
method of the EventChannel
instance:
1 |
myEventChannel.invokeMethod('sendMessage', 'Hello, Android!'); |
In your Android code, handle the message using the MethodChannel
class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
private static final String CHANNEL_NAME = "my_event_channel"; private static final String METHOD_NAME = "sendMessage"; private MethodChannel methodChannel; @Override public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { super.configureFlutterEngine(flutterEngine); methodChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL_NAME); methodChannel.setMethodCallHandler( (call, result) -> { if (call.method.equals(METHOD_NAME)) { // Receive message from Flutter app String message = call.argument("message"); myEventChannel.sendEvent(message); result.success(null); } else { result.notImplemented(); } } ); } |
That’s all from my side. You can check the official documentation on the Event Channel to learn more. Also, check Method Channel in Flutter.