A PlatformException means a Dart method channel call reached the native boundary and something went wrong on the other side — the channel isn't registered, the platform method threw, or the plugin isn't built into the binary.
The Error
PlatformException(channel-error, Unable to establish connection on channel, null, null)
Fix 1: Rebuild After Adding Native Code
flutter clean && flutter pub get
cd ios && pod install && cd ..
flutter run # full rebuild registers the channelFix 2: Always Catch PlatformException
try {
final result = await platform.invokeMethod('getBatteryLevel');
} on PlatformException catch (e) {
debugPrint('Native call failed: ${e.code} ${e.message}');
// show a graceful fallback to the user
}Fix 3: Check Platform Availability
If you call a channel that only exists on Android while running on iOS (or web), it errors. Guard with Platform.isAndroid/kIsWeb before invoking platform-specific methods.
Solution
After adding any plugin, fully stop and re-run the app. Method channels are registered at startup — hot reload won't wire up a brand-new native channel.
