0
0
Fluttermobile~10 mins

Why platform channels bridge native code in Flutter - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to send a message from Flutter to native code using platform channels.

Flutter
static const platform = MethodChannel('[1]');
Drag options to blanks, or click blank then click option'
Acom.example.app/channel
Bflutter/native
Capp/platform
Dnative/bridge
Attempts:
3 left
💡 Hint
Common Mistakes
Using a channel name that does not match the native side.
Leaving the channel name empty or generic.
2fill in blank
medium

Complete the code to invoke a native method called 'getBatteryLevel' from Flutter.

Flutter
final int batteryLevel = await platform.invokeMethod('[1]');
Drag options to blanks, or click blank then click option'
AreadBattery
BfetchBattery
CbatteryStatus
DgetBatteryLevel
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist on the native side.
Misspelling the method name.
3fill in blank
hard

Fix the error in the native Android code to receive messages from Flutter on the platform channel.

Flutter
new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(), "[1]")
  .setMethodCallHandler((call, result) -> {
    if (call.method.equals("getBatteryLevel")) {
      int batteryLevel = getBatteryLevel();
      if (batteryLevel != -1) {
        result.success(batteryLevel);
      } else {
        result.error("UNAVAILABLE", "Battery level not available.", null);
      }
    } else {
      result.notImplemented();
    }
  });
Drag options to blanks, or click blank then click option'
Acom.example.app/channel
Bflutter/native
Cnative/bridge
Dapp/platform
Attempts:
3 left
💡 Hint
Common Mistakes
Using different channel names on Flutter and native sides.
Typos in the channel name string.
4fill in blank
hard

Fill both blanks to complete the Flutter code that listens for native method calls and responds.

Flutter
platform.setMethodCallHandler((call) async {
  if (call.method == '[1]') {
    final String response = await getNativeData();
    return [2].success(response);
  } else {
    return [2].notImplemented();
  }
});
Drag options to blanks, or click blank then click option'
AfetchData
Bresult
Ccall
Dresponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names that don't match native calls.
Confusing the call object with the result object.
5fill in blank
hard

Fill all three blanks to create a platform channel in Flutter that sends a message and handles exceptions.

Flutter
try {
  final int result = await platform.invokeMethod('[1]');
  print('Result: ' + result.toString());
} on [2] catch (e) {
  print('Failed to get result: ' + e.[3]);
}
Drag options to blanks, or click blank then click option'
AgetBatteryLevel
BPlatformException
Cmessage
DMissingPluginException
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong exception types.
Accessing wrong properties of the exception.