EventChannel: Streaming Data from Native to Flutter
EventChannel is a one-way radio from native code to your Flutter app, designed for continuous event streams, not single method calls. Use it to listen for ongoing platform events like sensor data, GPS location updates, or battery state changes.
WHY IT EXISTS Flutter apps often need to react to a continuous flow of information from the underlying native platform, like sensor readings or connectivity status. A simple request-response model is inefficient for this. EventChannel provides a dedicated, one-way stream for this purpose, from the native platform to your Dart code.
THE MENTAL MODEL An EventChannel is like subscribing to a newsletter from the native platform. Your Flutter app says, "tell me whenever X happens," and the native code sends a message down the channel every time it does. It's a one-way broadcast from native (Android/iOS) to Dart, ideal for data that arrives over time.
HOW IT WORKS You create an EventChannel in Dart with a unique string name. On the native side (Kotlin/Java or Swift/Objective-C), you set up a corresponding channel handler with the exact same name. In Dart, you call receiveBroadcastStream() on the channel instance. This returns a Stream you can listen to. When the native code wants to send data, it emits an event, which is encoded, sent over the channel, and decoded into a Dart object for your listener.
WHEN TO USE IT Use EventChannel when you need to receive a stream of data from the platform. This is perfect for cases like: first, listening to hardware sensor events (e.g., accelerometer); second, receiving continuous location updates from GPS; third, monitoring battery level or charging state; and fourth, getting notifications about network connectivity changes.
WHEN NOT TO USE IT Do not use EventChannel for single, two-way conversations where your Dart code needs a direct response to a specific request. For that, use a MethodChannel. EventChannel is for subscribing to platform-initiated events, not for invoking a single function and getting a single result back.
ONE CANONICAL EXAMPLE A fitness app needs to display the user's step count in real-time. The native platform's pedometer API provides a continuous stream of updates. An EventChannel named com.example.app/steps is created. The Flutter UI listens to the stream from this channel. The native code registers with the pedometer sensor and, on every update, sends the new step count over the event channel. The Flutter UI receives the new count and updates the display.
Read the original → api.flutter.dev
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.