From 811701341be489a9d1b8e0cb82df74b58a641e3a Mon Sep 17 00:00:00 2001 From: Roshan Date: Sun, 7 Sep 2025 01:17:01 +0530 Subject: [PATCH] fix(android): Prevent potential crash in checkPowerServiceSaveMode The checkPowerServiceSaveMode method used a non-null assertion (!!) on applicationContext. This could potentially lead to a NullPointerException in rare edge cases. This change replaces the assertion with a safe-call block (`?.let`), ensuring the method safely returns `false` if the context is null, thereby improving the plugin's robustness. --- .../dev/fluttercommunity/plus/battery/BatteryPlusPlugin.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/battery_plus/battery_plus/android/src/main/kotlin/dev/fluttercommunity/plus/battery/BatteryPlusPlugin.kt b/packages/battery_plus/battery_plus/android/src/main/kotlin/dev/fluttercommunity/plus/battery/BatteryPlusPlugin.kt index 431a44ffa5..b6be0d24e0 100644 --- a/packages/battery_plus/battery_plus/android/src/main/kotlin/dev/fluttercommunity/plus/battery/BatteryPlusPlugin.kt +++ b/packages/battery_plus/battery_plus/android/src/main/kotlin/dev/fluttercommunity/plus/battery/BatteryPlusPlugin.kt @@ -170,9 +170,10 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter } private fun checkPowerServiceSaveMode(): Boolean { - val powerManager = - applicationContext!!.getSystemService(Context.POWER_SERVICE) as PowerManager - return powerManager.isPowerSaveMode + return applicationContext?.let { + val powerManager = it.getSystemService(Context.POWER_SERVICE) as PowerManager + powerManager.isPowerSaveMode + } ?: false } private fun getBatteryProperty(property: Int): Int {