Skip to content

Commit

Permalink
fix(battery_plus): Fix return value of getBattery to be nullable (#2745)
Browse files Browse the repository at this point in the history
  • Loading branch information
koji-1009 authored Mar 21, 2024
1 parent a73af89 commit 4d5b950
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 86 deletions.
129 changes: 45 additions & 84 deletions packages/battery_plus/battery_plus/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,9 @@ class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
try {
_battery.batteryState.then(
_updateBatteryState,
onError: (e) {
_showError('onError: batteryState: $e');
_updateBatteryState(BatteryState.unknown);
},
);
_batteryStateSubscription = _battery.onBatteryStateChanged.listen(
_updateBatteryState,
onError: (e) {
_showError('onError: onBatteryStateChanged: $e');
_updateBatteryState(BatteryState.unknown);
},
);
} on Error catch (e) {
_showError('catch: batteryState: $e');
}
_battery.batteryState.then(_updateBatteryState);
_batteryStateSubscription =
_battery.onBatteryStateChanged.listen(_updateBatteryState);
}

void _updateBatteryState(BatteryState state) {
Expand All @@ -69,16 +54,6 @@ class _MyHomePageState extends State<MyHomePage> {
});
}

void _showError(String message) {
// see https://github.com/fluttercommunity/plus_plugins/pull/2720
// The exception may not be caught in the package and an exception may occur in the caller, so use try-catch as needed.
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
),
);
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -102,69 +77,55 @@ class _MyHomePageState extends State<MyHomePage> {
const SizedBox(height: 24),
ElevatedButton(
onPressed: () {
try {
_battery.batteryLevel.then(
(batteryLevel) {
showDialog<void>(
context: context,
builder: (_) => AlertDialog(
content: Text('Battery: $batteryLevel%'),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('OK'),
)
],
),
);
},
onError: (e) {
_showError('onError: batteryLevel: $e');
},
);
} on Error catch (e) {
_showError('catch: batteryLevel: $e');
}
_battery.batteryLevel.then(
(batteryLevel) {
showDialog<void>(
context: context,
builder: (_) => AlertDialog(
content: Text('Battery: $batteryLevel%'),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('OK'),
)
],
),
);
},
);
},
child: const Text('Get battery level'),
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: () {
try {
_battery.isInBatterySaveMode.then(
(isInPowerSaveMode) {
showDialog<void>(
context: context,
builder: (_) => AlertDialog(
title: const Text(
'Is in Battery Save mode?',
style: TextStyle(fontSize: 20),
),
content: Text(
"$isInPowerSaveMode",
style: const TextStyle(fontSize: 18),
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Close'),
)
],
_battery.isInBatterySaveMode.then(
(isInPowerSaveMode) {
showDialog<void>(
context: context,
builder: (_) => AlertDialog(
title: const Text(
'Is in Battery Save mode?',
style: TextStyle(fontSize: 20),
),
content: Text(
"$isInPowerSaveMode",
style: const TextStyle(fontSize: 18),
),
);
},
onError: (e) {
_showError('onError: isInBatterySaveMode: $e');
},
);
} on Error catch (e) {
_showError('catch: isInBatterySaveMode: $e');
}
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Close'),
)
],
),
);
},
);
},
child: const Text('Is in Battery Save mode?'),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BatteryPlusWebPlugin extends BatteryPlatform {
/// Return [BatteryManager] if the BatteryManager API is supported by the User Agent.
Future<BatteryManager?> _getBatteryManager() async {
try {
return await web.window.navigator.getBattery().toDart;
return await web.window.navigator.getBattery()?.toDart;
} on NoSuchMethodError catch (_) {
// BatteryManager API is not supported this User Agent.
return null;
Expand Down Expand Up @@ -108,7 +108,7 @@ class BatteryPlusWebPlugin extends BatteryPlatform {

extension on web.Navigator {
/// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getBattery
external JSPromise<BatteryManager> getBattery();
external JSPromise<BatteryManager>? getBattery();
}

/// BatteryManager API
Expand Down

0 comments on commit 4d5b950

Please sign in to comment.