Complete the code to send a message from Flutter to native code using platform channels.
static const platform = MethodChannel('[1]');
The channel name must match on both Flutter and native sides to communicate properly.
Complete the code to invoke a native method called 'getBatteryLevel' from Flutter.
final int batteryLevel = await platform.invokeMethod('[1]');
The method name must exactly match the native method name to call it correctly.
Fix the error in the native Android code to receive messages from Flutter on the platform channel.
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(); } });
The channel name must match the one used in Flutter to establish communication.
Fill both blanks to complete the Flutter code that listens for native method calls and responds.
platform.setMethodCallHandler((call) async {
if (call.method == '[1]') {
final String response = await getNativeData();
return [2].success(response);
} else {
return [2].notImplemented();
}
});The method name must match the native call, and the result object is used to send responses back.
Fill all three blanks to create a platform channel in Flutter that sends a message and handles exceptions.
try { final int result = await platform.invokeMethod('[1]'); print('Result: ' + result.toString()); } on [2] catch (e) { print('Failed to get result: ' + e.[3]); }
The method name is 'getBatteryLevel'. The exception type is PlatformException. The error message is accessed via the 'message' property.