0
0
Fluttermobile~20 mins

Platform channels (MethodChannel) in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Platform Channel Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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
ABattery level is 85%
BBattery level is null%
CBattery level is 0%
DError: Method not found
Attempts:
2 left
💡 Hint
Think about what the native side returns and how Flutter receives it.
lifecycle
intermediate
2: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?
AInside the <code>main()</code> function before runApp()
BInside the <code>build()</code> method of a StatelessWidget
CInside the <code>dispose()</code> method
DInside the <code>initState()</code> method of a StatefulWidget
Attempts:
2 left
💡 Hint
Think about when the widget is first created and ready to handle messages.
📝 Syntax
advanced
2: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%');
}
ASyntaxError due to missing semicolons
BNo error, runs correctly
CRuntime error: Method not found
DTypeError: batteryLevel is null
Attempts:
2 left
💡 Hint
Check the punctuation carefully in Dart code.
navigation
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
  }
});
AAll of the above are correct if context is available
BNavigator.of(context).push(MaterialPageRoute(builder: (_) => DetailsPage()));
CNavigator.pushNamed(context, '/details');
DNavigator.push(context, MaterialPageRoute(builder: (_) => DetailsPage()));
Attempts:
2 left
💡 Hint
Think about Flutter navigation methods and context availability.
🔧 Debug
expert
2: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?
AFlutter does not have internet permission
BThe native code uses a different thread to send messages
CThe MethodChannel name strings do not match exactly between Flutter and native code
DThe Flutter app is missing a MaterialApp widget
Attempts:
2 left
💡 Hint
Check the channel name carefully in both Flutter and native code.