fluttercommunity / fluttercommunity/plus_plugins

[Request]: Stream for Battery Level Changes

Offen
#3,733 1 Kommentar 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen
enhancement triage
Vorherrschende Sprache
Dart
Sterne
1.9k
Forks
1.3k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

### Plugin

battery_plus

### Use case

Currently, `battery_plus` only provides a Future for `batteryLevel` and a Stream for `batteryState`. To track level changes, developers are forced to use `Timer.periodic` to poll the getter, which is inefficient and not reactive.

### Proposal

I would like to request a new stream: Stream onBatteryLevelChanged.

Platforms support this natively, so we should not need to rely on polling.

* iOS: We can observe `UIDevice.batteryLevelDidChangeNotification`. Note: Apple documentation states this notification is sent no more frequently than once per minute, which is acceptable for most use cases.
* Android: We can register a `BroadcastReceiver` for `Intent.ACTION_BATTERY_CHANGED`. This intent contains the EXTRA_LEVEL and EXTRA_SCALE to calculate the percentage immediately upon change.
* Web: The `BatteryManager` has a built-in event listener: `onlevelchange`.
* macOS: The system provides a system loop notification: `kIOPSPowerSourceHaveToUpdateNotification`

Something like:

```dart
// Current
Stream get onBatteryStateChanged;

// Proposed
Stream get onBatteryLevelChanged;
```

### Additional Context

* This issue streamlines the battery level, but a stream to listen to all battery aspects would the preferrable solution. See #3446

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.