MethodChannel data flow and type marshalling
how platform channels marshal data.
Dart invokeMethod sends a StandardMessageCodec-serialized payload to native, which returns a primitive or map; complex objects must be reduced to supported types.
What's really being asked
Whether you grasp the asynchronous, serialized nature of platform channels and the constraints on what data can cross the Dart-to-native boundary.
The full answer
From Dart you call channel.invokeMethod with a method name and an optional arguments value. The StandardMethodCodec serializes the arguments into a binary message sent over the BinaryMessenger to the host platform. On Android a MethodCallHandler in Kotlin or Java receives the MethodCall, switches on call.method, runs native work, and replies via result.success, result.error, or result.notImplemented; iOS does the same in Swift or Objective-C. The reply is decoded back into Dart and completes the Future. Everything is asynchronous. Only codec-supported types travel: null, booleans, numbers, strings, Uint8List and similar byte buffers, and Lists and Maps composed of those.
The mistakes people make
Thinking you can pass a custom Dart class and have it appear as a native object automatically. Treating invokeMethod as synchronous. Forgetting to handle MissingPluginException or PlatformException. Assuming the return type is strongly typed; it arrives as dynamic and must be cast.
What usually comes next
How do you pass a custom object across. Difference between MethodChannel, EventChannel and BasicMessageChannel. What thread does the handler run on. How do errors propagate back as PlatformException.
A concrete example
To fetch battery level you call await channel.invokeMethod('getBatteryLevel'). The native handler reads the level and calls result.success(level), returning an int that arrives in Dart as a dynamic to cast. To send a user profile, you cannot pass a Dart User object; you convert it to a Map of primitives first, pass that, and reconstruct it natively, since the codec only understands primitives and collections of them.
Interview question
You pass a custom Dart object as an argument to invokeMethod and the native side cannot use it directly. Why?
- a.The platform codec only serializes primitives and collections, not arbitrary classesCorrect
- b.Native code only accepts JSON strings as arguments
- c.invokeMethod silently converts objects to byte buffers
- d.MethodChannel calls are synchronous and arguments are dropped
Why? this is the answer
The StandardMethodCodec supports null, primitives, strings, byte buffers, and Lists/Maps of those, so custom classes must be flattened to a Map first. Calls are also asynchronous, not synchronous as the first option claims.
Just read this? Test yourself on what you have been reading.
Read the original → docs.flutter.dev
- #flutter
- #methodchannel
- #platform-channels
- #native-interop
- #serialization
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.
See open roles