Sub topic 18
Compatibility, Permissions, and Security
Assume you know intermediate Dart and Flutter, can use Future and Stream, and have basic Kotlin or Swift experience. Platform channels cross an application boundary, so compatibility and security need deliberate design.
Check Capabilities Before Calling Native Code
Flutter code can run on Android, iOS, desktop, and web. A channel handler may exist on only one platform. Calling an unavailable method can produce MissingPluginException, while a native handler can return notImplemented.
Treat platform support as a capability. Check the platform and handle unsupported features without crashing:
import 'dart:io';
import 'package:flutter/services.dart';
class BatteryApi {
static const _channel = MethodChannel('com.example.app/battery');
Future<int?> readLevel() async {
if (!Platform.isAndroid && !Platform.isIOS) {
return null;
}
try {
return await _channel.invokeMethod<int>('readLevel');
} on MissingPluginException {
return null;
} on PlatformException catch (error) {
if (error.code == 'PERMISSION_DENIED') {
return null;
}
rethrow;
}
}
}Use a stable channel name and method contract. If the contract changes, consider a versioned method or a capability method such as supportsFeature. Do not assume that a newer Dart package means every installed native binary has the matching implementation.
Request Only the Permissions You Need
Some native APIs require runtime permissions. Examples include location, Bluetooth, camera, microphone, contacts, and notifications. Declare permissions in the platform project, then request them at the appropriate time. A channel handler should still check the current permission state because the user can revoke access later.
Explain the feature before requesting access. If the user denies permission, return a stable error code such as PERMISSION_DENIED, rather than exposing platform-specific wording to the Dart layer. The Dart wrapper can map that code to a user-facing action.
Permissions do not grant unlimited access. Apply platform restrictions, such as iOS usage descriptions, Android manifest declarations, background execution rules, and restricted API requirements.
Protect the Channel Boundary
Never trust values received from Dart or from native code. Validate types, ranges, lengths, and identifiers on both sides. Avoid passing secrets through logs, channel arguments, or error details. A channel is usually internal to the app, but compromised or modified app code can still call it.
Use the smallest native API surface possible. Keep authentication, authorization, and sensitive storage in native or server-side components when appropriate. Do not treat a hidden method name as a security control.
Test Compatibility Paths
Test the Dart wrapper with mocked method calls for success, unsupported methods, invalid arguments, permission denial, and native exceptions. Add integration tests on each supported platform because mocks cannot prove that handlers are registered correctly or that platform permissions are configured.
Exercise
Why should a Dart wrapper handle MissingPluginException and PERMISSION_DENIED separately?
Show answer
The first indicates that the native implementation is unavailable; the second indicates that the feature exists but the user or system has denied access.