From 1bbbd5c4f6e4b16081d0be4651ae8879ce781e0f Mon Sep 17 00:00:00 2001 From: xioxin <5716100+xioxin@users.noreply.github.com> Date: Thu, 8 Aug 2024 16:29:27 +0800 Subject: [PATCH] fix(battery_plus): handle empty power sources array --- .../battery_plus/macos/Classes/BatteryPlusMacosPlugin.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/battery_plus/battery_plus/macos/Classes/BatteryPlusMacosPlugin.swift b/packages/battery_plus/battery_plus/macos/Classes/BatteryPlusMacosPlugin.swift index a323d6a474..246336c034 100644 --- a/packages/battery_plus/battery_plus/macos/Classes/BatteryPlusMacosPlugin.swift +++ b/packages/battery_plus/battery_plus/macos/Classes/BatteryPlusMacosPlugin.swift @@ -48,6 +48,9 @@ public class BatteryPlusMacosPlugin: NSObject, FlutterPlugin { private func getBatteryLevel()-> Int { let powerSourceSnapshot = IOPSCopyPowerSourcesInfo().takeRetainedValue() let sources = IOPSCopyPowerSourcesList(powerSourceSnapshot).takeRetainedValue() as Array + if sources.isEmpty { + return -1 + } let description = IOPSGetPowerSourceDescription(powerSourceSnapshot, sources[0]).takeUnretainedValue() as! [String: AnyObject] if let currentCapacity = description[kIOPSCurrentCapacityKey] as? Int { return currentCapacity;