这是indexloc提供的服务,不要输入任何密码
Skip to content

fix(battery_plus): battery state always unknown (iOS only) #2481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2024

Conversation

rostopira
Copy link
Contributor

@rostopira rostopira commented Dec 29, 2023

Description

If battery percentage never read battery state is always unknown

Related Issues

None

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

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (please indicate that with a ! in the title as explained in Conventional Commits).
  • No, this is not a breaking change.

If battery percentage never read battery state is always unknown
@rostopira rostopira changed the title fix(ios_battery_plus): battery state always unknown fix(battery_plus): battery state always unknown Dec 29, 2023
@rostopira rostopira changed the title fix(battery_plus): battery state always unknown fix(battery_plus): battery state always unknown (iOS only) Dec 29, 2023
@vbuberen
Copy link
Collaborator

vbuberen commented Jan 9, 2024

Thank you for your contribution.

Could you elaborate more on what is going on? Didn't understand from your description what is wrong there.

@vbuberen
Copy link
Collaborator

@rostopira Could you reply to ☝🏻 ?

@rostopira
Copy link
Contributor Author

rostopira commented Jan 30, 2024

@vbuberen sorry, missed previous comment.

There is a steps to reproduce a bug

import 'package:battery_plus/battery_plus.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final batteryState = await Battery().batteryState;
  print(batteryState);
}

If you run it on iOS without my patch you will get BatteryState.unknown instead of the real battery state.
It may be reproducible on macOS as well, but I didn't tried that.

But if you call await Battery().batteryLevel before reading batteryState it will work as expected.
That's because reading battery level read implementation has device.batteryMonitoringEnabled = YES; and battery state doesn't.
I've just added those missing lines, check the code it's really only 3 lines.

@vbuberen
Copy link
Collaborator

vbuberen commented Feb 4, 2024

Thanks a lot for detailed explanations and reproducer. I was even able to reproduce the problem in the example app we have for the plugin. And indeed your patch works. Approving and will release this fix the upcoming week.

Copy link
Collaborator

@vbuberen vbuberen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution.

@vbuberen vbuberen merged commit ea892fb into fluttercommunity:main Feb 4, 2024
@vbuberen
Copy link
Collaborator

vbuberen commented Feb 4, 2024

On MacOS couldn't get the same bug, so it seems applies only to iOS.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 4, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants