Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 4 additions & 22 deletions packages/battery_plus/battery_plus/lib/src/battery_plus_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:js_interop';

import 'package:battery_plus_platform_interface/battery_plus_platform_interface.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:web/web.dart' as web;
import 'package:web/web.dart';

/// The web implementation of the BatteryPlatform of the Battery plugin.
///
Expand All @@ -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 window.navigator.getBattery().toDart;
} on NoSuchMethodError catch (_) {
// BatteryManager API is not supported this User Agent.
return null;
Expand All @@ -44,7 +44,7 @@ class BatteryPlusWebPlugin extends BatteryPlatform {

// level is a number representing the system's battery charge level scaled to a value between 0.0 and 1.0
final level = batteryManager.level;
return level * 100 as int;
return (level * 100).toInt();
}

/// Returns the current battery state.
Expand Down Expand Up @@ -80,7 +80,7 @@ class BatteryPlusWebPlugin extends BatteryPlatform {
_checkBatteryChargingState(batteryManager.charging),
);

batteryManager.onchargingchange = (web.Event _) {
batteryManager.onchargingchange = (Event _) {
_batteryChangeStreamController?.add(
_checkBatteryChargingState(batteryManager.charging),
);
Expand All @@ -105,21 +105,3 @@ class BatteryPlusWebPlugin extends BatteryPlatform {
}
}
}

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

/// BatteryManager API
/// https://developer.mozilla.org/en-US/docs/Web/API/BatteryManager
extension type BatteryManager(JSObject _) implements JSObject {
/// https://developer.mozilla.org/en-US/docs/Web/API/BatteryManager/level
external double get level;

/// https://developer.mozilla.org/en-US/docs/Web/API/BatteryManager/charging
external bool get charging;

/// https://developer.mozilla.org/en-US/docs/Web/API/BatteryManager/chargingchange_event
external set onchargingchange(JSFunction fn);
}
2 changes: 1 addition & 1 deletion packages/battery_plus/battery_plus/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies:
battery_plus_platform_interface: ^2.0.1
meta: ^1.8.0
upower: ^0.7.0
web: ^0.5.0
web: ^1.0.0

dev_dependencies:
flutter_test:
Expand Down