tezvyn:

When to choose EventChannel over MethodChannel and native setup

AI-drafted, machine-checkedSource: docs.flutter.devintermediate

It tests streaming versus one-shot platform communication. Pick EventChannel for ongoing native events like sensor data; register a StreamHandler on Android or iOS that feeds an event sink, then consume in Dart via receiveBroadcastStream.

WHAT THIS TESTS: This question tests whether you can distinguish between discrete request-response interactions and persistent unidirectional data flows across the Flutter platform channel boundary. Interviewers want to see that you know MethodChannel is designed for one-off async invocations while EventChannel is purpose-built for streams of data that originate on the native side and are consumed reactively in Dart.

A GOOD ANSWER COVERS: A good answer hits four things in order. First, the decision criteria: choose EventChannel when the native layer produces a continuous sequence of events such as sensor readings, GPS coordinates, or Bluetooth status updates where the Dart side is a passive consumer. Second, native setup: on Android you create an EventChannel in the MainActivity or plugin and set a StreamHandler that receives an EventSink; on iOS you use FlutterEventChannel and implement FlutterStreamHandler to obtain a FlutterEventSink. Third, emission mechanics: the native side pushes data by calling success on the sink whenever new data arrives, and it must handle the sink being null before the first listener attaches. Fourth, Dart consumption: you instantiate an EventChannel with the exact same name, call receiveBroadcastStream to get a Stream, and listen to it with normal Stream APIs while handling onCancel to clean up native resources when the last listener detaches.

COMMON WRONG ANSWERS: The biggest red flag is suggesting a Timer that repeatedly calls MethodChannel to simulate streaming. This shows a misunderstanding of backpressure and battery efficiency. Another mistake is forgetting stream lifecycle management, such as not removing the native listener when the Dart subscription cancels, which causes memory leaks and events firing into a void. Some candidates also confuse two-way communication with EventChannel; it is not a duplex channel for sending commands from Dart to native, it is strictly native-to-Dart streaming.

LIKELY FOLLOW-UPS: Interviewers often ask how you would handle backpressure if native emits faster than Dart consumes. They may also ask how to pass arguments when setting up the stream, which is done by providing a codec-compatible argument to receiveBroadcastStream. Another common follow-up is error propagation: explain how you call error on the native event sink and map it to Dart stream errors.

ONE CONCRETE EXAMPLE: A practical example is reading the device accelerometer at sixty hertz. On the Android side you register a SensorEventListener, create an EventChannel named com.example.sensors.accel, and in your StreamHandler onListen callback you attach to the hardware sensor and forward SensorEvent values to the EventSink. On the Dart side you create an EventChannel with the identical name, invoke receiveBroadcastStream, and map the incoming list of doubles to an AccelerometerData object. When the widget disposes you cancel the stream subscription, which triggers onCancel on the native side so you unregister the SensorEventListener and stop the hardware to save power.

Read the original → docs.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.