Skip to content

Commit 8117013

Browse files
authored
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.
1 parent b49befe commit 8117013

File tree

1 file changed

+4
-3
lines changed
  • packages/battery_plus/battery_plus/android/src/main/kotlin/dev/fluttercommunity/plus/battery

1 file changed

+4
-3
lines changed

packages/battery_plus/battery_plus/android/src/main/kotlin/dev/fluttercommunity/plus/battery/BatteryPlusPlugin.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,10 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter
170170
}
171171

172172
private fun checkPowerServiceSaveMode(): Boolean {
173-
val powerManager =
174-
applicationContext!!.getSystemService(Context.POWER_SERVICE) as PowerManager
175-
return powerManager.isPowerSaveMode
173+
return applicationContext?.let {
174+
val powerManager = it.getSystemService(Context.POWER_SERVICE) as PowerManager
175+
powerManager.isPowerSaveMode
176+
} ?: false
176177
}
177178

178179
private fun getBatteryProperty(property: Int): Int {

0 commit comments

Comments
 (0)