Challenge - 5 Problems
Platform Channel Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output when calling a native method with MethodChannel?
Consider this Flutter code snippet that calls a native method named
getBatteryLevel using MethodChannel. What will be the output shown in the Flutter app if the native side returns 85?Flutter
final MethodChannel channel = MethodChannel('samples.flutter.dev/battery'); Future<void> getBatteryLevel() async { final int batteryLevel = await channel.invokeMethod('getBatteryLevel'); print('Battery level is $batteryLevel%'); } // Assume getBatteryLevel() is called and native returns 85
Attempts:
2 left
💡 Hint
Think about what the native side returns and how Flutter receives it.
✗ Incorrect
The Flutter app calls the native method via MethodChannel. If the native side returns 85, Flutter receives it as an integer and prints 'Battery level is 85%'.
❓ lifecycle
intermediate2:00remaining
When should you set up the MethodChannel listener in Flutter?
In a Flutter app using MethodChannel to receive messages from native code, when is the best lifecycle moment to set up the
setMethodCallHandler listener to avoid missing messages?Attempts:
2 left
💡 Hint
Think about when the widget is first created and ready to handle messages.
✗ Incorrect
Setting up the listener inside initState() ensures it is ready when the widget is active and avoids missing messages. build() can be called multiple times, dispose() is too late, and main() is before Flutter widgets exist.
📝 Syntax
advanced2:00remaining
What error does this Flutter MethodChannel code produce?
Examine this Flutter code snippet using MethodChannel. What error will it cause when run?
Flutter
final MethodChannel channel = MethodChannel('samples.flutter.dev/battery'); Future<void> getBatteryLevel() async { final int batteryLevel = await channel.invokeMethod('getBatteryLevel'); print('Battery level is $batteryLevel%'); }
Attempts:
2 left
💡 Hint
Check the punctuation carefully in Dart code.
✗ Incorrect
Dart requires semicolons at the end of statements. Missing semicolons cause syntax errors.
advanced
2:00remaining
How to navigate after receiving a native message via MethodChannel?
You receive a message from native code via MethodChannel in Flutter. You want to navigate to a new screen named
DetailsPage when the message arrives. Which code snippet correctly performs this navigation inside the setMethodCallHandler?Flutter
channel.setMethodCallHandler((call) async {
if (call.method == 'showDetails') {
// Navigate to DetailsPage
}
});Attempts:
2 left
💡 Hint
Think about Flutter navigation methods and context availability.
✗ Incorrect
Option B uses the correct syntax: `Navigator.of(context).push(...)`. Options B and C are invalid because `Navigator` does not have static `push` or `pushNamed` methods. Option B is incorrect. The key is having `BuildContext` accessible inside the handler.
🔧 Debug
expert2:00remaining
Why does Flutter not receive native messages via MethodChannel?
A developer sets up a MethodChannel in Flutter and native Android code to send messages. However, Flutter never receives the messages. Which is the most likely cause?
Attempts:
2 left
💡 Hint
Check the channel name carefully in both Flutter and native code.
✗ Incorrect
MethodChannel communication depends on matching channel names exactly. If they differ, messages won't be received.