Skip to content

fix(battery_plus): invoke client.close() on Linux batteryState getter - #3967

Open
chiliec wants to merge 1 commit into
fluttercommunity:mainfrom
chiliec:fix/battery-plus-linux-close-client
Open

fix(battery_plus): invoke client.close() on Linux batteryState getter#3967
chiliec wants to merge 1 commit into
fluttercommunity:mainfrom
chiliec:fix/battery-plus-linux-close-client

Conversation

@chiliec

@chiliec chiliec commented Sep 3, 2026

Copy link
Copy Markdown

Description

On Linux, the batteryState getter passes a tear-off (client.close) to
whenComplete instead of invoking it (client.close()), so the UPower client
is never closed after reading the battery state. The sibling batteryLevel
getter already calls client.close() correctly — this brings batteryState
in line with it.

// before
.whenComplete(() => client.close);
// after
.whenComplete(() => client.close());

Related Issues

Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I titled the PR using Conventional Commits.
  • I did not modify the CHANGELOG.md nor the plugin version in pubspec.yaml files.
  • All existing and new tests are passing.
  • The analyzer (flutter analyze) does not report any problems on my PR.

Breaking Change

  • Yes, this is a breaking change.
  • No, this is not a breaking change.

Tests

Added a regression test (battery state closes the client) to
battery_plus_linux_test.dart that verifies client.close() is invoked once
after batteryState completes.

Verified genuine RED→GREEN with Flutter 3.47.2 (Dart 3.13.2) on Linux:

  • Before the fix: verify(client.close()).called(1) fails —
    No matching calls. All calls: MockUPowerClient.connect(), MockUPowerClient.displayDevice.
  • After the fix: all 5 tests pass.
$ flutter test test/battery_plus_linux_test.dart
00:00 +5: All tests passed!

$ flutter analyze lib/src/battery_plus_linux.dart test/battery_plus_linux_test.dart
No issues found!

Happy to adjust if you'd prefer a different test shape.


AI disclosure: this change was prepared with AI assistance and verified locally as described above.

The Linux batteryState getter passed a tear-off (client.close) to
whenComplete instead of calling it (client.close()), so the UPower
client was never closed after reading the state. The sibling
batteryLevel getter already calls client.close() correctly.
@chiliec

chiliec commented Sep 3, 2026

Copy link
Copy Markdown
Author

Heads up on CI: the red Check code formatting job is not from this PR — it only flags packages/share_plus/share_plus/test/share_plus_windows_test.dart, which is unchanged here and already reformats on a clean main checkout (newer dart formatter than the repo was last formatted with). The formatting job reports battery_plus: SUCCESS, and dart format --set-exit-if-changed on the two files this PR touches reports 0 changed.

The test, analyze, Dart Analyzer, linux_example_build and linux_integration_test checks (the ones relevant to this Linux-only Dart change) are green. The remaining red checks (ios/macos/windows example builds, pub.dev requirements) are platform/infra jobs unrelated to this change. Happy to rebase if you'd like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Linux bug on batteryState getter

1 participant