Skip to content

Commit 4d5b950

Browse files
authored
fix(battery_plus): Fix return value of getBattery to be nullable (#2745)
1 parent a73af89 commit 4d5b950

File tree

2 files changed

+47
-86
lines changed

2 files changed

+47
-86
lines changed

packages/battery_plus/battery_plus/example/lib/main.dart

Lines changed: 45 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,9 @@ class _MyHomePageState extends State<MyHomePage> {
4242
@override
4343
void initState() {
4444
super.initState();
45-
try {
46-
_battery.batteryState.then(
47-
_updateBatteryState,
48-
onError: (e) {
49-
_showError('onError: batteryState: $e');
50-
_updateBatteryState(BatteryState.unknown);
51-
},
52-
);
53-
_batteryStateSubscription = _battery.onBatteryStateChanged.listen(
54-
_updateBatteryState,
55-
onError: (e) {
56-
_showError('onError: onBatteryStateChanged: $e');
57-
_updateBatteryState(BatteryState.unknown);
58-
},
59-
);
60-
} on Error catch (e) {
61-
_showError('catch: batteryState: $e');
62-
}
45+
_battery.batteryState.then(_updateBatteryState);
46+
_batteryStateSubscription =
47+
_battery.onBatteryStateChanged.listen(_updateBatteryState);
6348
}
6449

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

72-
void _showError(String message) {
73-
// see https://github.com/fluttercommunity/plus_plugins/pull/2720
74-
// The exception may not be caught in the package and an exception may occur in the caller, so use try-catch as needed.
75-
ScaffoldMessenger.of(context).showSnackBar(
76-
SnackBar(
77-
content: Text(message),
78-
),
79-
);
80-
}
81-
8257
@override
8358
Widget build(BuildContext context) {
8459
return Scaffold(
@@ -102,69 +77,55 @@ class _MyHomePageState extends State<MyHomePage> {
10277
const SizedBox(height: 24),
10378
ElevatedButton(
10479
onPressed: () {
105-
try {
106-
_battery.batteryLevel.then(
107-
(batteryLevel) {
108-
showDialog<void>(
109-
context: context,
110-
builder: (_) => AlertDialog(
111-
content: Text('Battery: $batteryLevel%'),
112-
actions: <Widget>[
113-
TextButton(
114-
onPressed: () {
115-
Navigator.pop(context);
116-
},
117-
child: const Text('OK'),
118-
)
119-
],
120-
),
121-
);
122-
},
123-
onError: (e) {
124-
_showError('onError: batteryLevel: $e');
125-
},
126-
);
127-
} on Error catch (e) {
128-
_showError('catch: batteryLevel: $e');
129-
}
80+
_battery.batteryLevel.then(
81+
(batteryLevel) {
82+
showDialog<void>(
83+
context: context,
84+
builder: (_) => AlertDialog(
85+
content: Text('Battery: $batteryLevel%'),
86+
actions: <Widget>[
87+
TextButton(
88+
onPressed: () {
89+
Navigator.pop(context);
90+
},
91+
child: const Text('OK'),
92+
)
93+
],
94+
),
95+
);
96+
},
97+
);
13098
},
13199
child: const Text('Get battery level'),
132100
),
133101
const SizedBox(height: 24),
134102
ElevatedButton(
135103
onPressed: () {
136-
try {
137-
_battery.isInBatterySaveMode.then(
138-
(isInPowerSaveMode) {
139-
showDialog<void>(
140-
context: context,
141-
builder: (_) => AlertDialog(
142-
title: const Text(
143-
'Is in Battery Save mode?',
144-
style: TextStyle(fontSize: 20),
145-
),
146-
content: Text(
147-
"$isInPowerSaveMode",
148-
style: const TextStyle(fontSize: 18),
149-
),
150-
actions: <Widget>[
151-
TextButton(
152-
onPressed: () {
153-
Navigator.pop(context);
154-
},
155-
child: const Text('Close'),
156-
)
157-
],
104+
_battery.isInBatterySaveMode.then(
105+
(isInPowerSaveMode) {
106+
showDialog<void>(
107+
context: context,
108+
builder: (_) => AlertDialog(
109+
title: const Text(
110+
'Is in Battery Save mode?',
111+
style: TextStyle(fontSize: 20),
112+
),
113+
content: Text(
114+
"$isInPowerSaveMode",
115+
style: const TextStyle(fontSize: 18),
158116
),
159-
);
160-
},
161-
onError: (e) {
162-
_showError('onError: isInBatterySaveMode: $e');
163-
},
164-
);
165-
} on Error catch (e) {
166-
_showError('catch: isInBatterySaveMode: $e');
167-
}
117+
actions: <Widget>[
118+
TextButton(
119+
onPressed: () {
120+
Navigator.pop(context);
121+
},
122+
child: const Text('Close'),
123+
)
124+
],
125+
),
126+
);
127+
},
128+
);
168129
},
169130
child: const Text('Is in Battery Save mode?'),
170131
)

packages/battery_plus/battery_plus/lib/src/battery_plus_web.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class BatteryPlusWebPlugin extends BatteryPlatform {
1818
/// Return [BatteryManager] if the BatteryManager API is supported by the User Agent.
1919
Future<BatteryManager?> _getBatteryManager() async {
2020
try {
21-
return await web.window.navigator.getBattery().toDart;
21+
return await web.window.navigator.getBattery()?.toDart;
2222
} on NoSuchMethodError catch (_) {
2323
// BatteryManager API is not supported this User Agent.
2424
return null;
@@ -108,7 +108,7 @@ class BatteryPlusWebPlugin extends BatteryPlatform {
108108

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

114114
/// BatteryManager API

0 commit comments

Comments
 (0)