Skip to content
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

feat(battery_plus)!: Migrate to package:web #2720

Merged
merged 2 commits into from Mar 19, 2024

Conversation

koji-1009
Copy link
Contributor

Description

Migration to the web package and add missing API.

https://developer.mozilla.org/en-US/docs/Web/API/BatteryManager#browser_compatibility

BatteryManager API is only supported Chrome or Edge.
Safari and Firefox are not supported, so status is expected to be unknown.

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

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.

@miquelbeltran miquelbeltran changed the title feat(share_plus)!: Migrate to package:web feat(battery_plus)!: Migrate to package:web Mar 17, 2024
@JgomesAT

This comment was marked as off-topic.

@miquelbeltran
Copy link
Member

when will be available a new version in pub.dev?

Please check the pinned issue: #2653

@koji-1009
Copy link
Contributor Author

I am working on a strange problem.

In the case of flutter run or flutter run --release, we can try-catch the NoSuchMethodError that occurs on window.navigator.getWindow() with battery_plus_web.dart.

  bool get isSupported {
    try {
      web.window.navigator.getBattery();
      return true;
    } on NoSuchMethodError catch (_) {
      // flutter run mode, catch the exception
      return false;
    }
  }

However, if we have a flutter build, we cannot try-catch it in battery_plus_web.dart, but have to try-catch it in main.dart (in example).

  @override
  void initState() {
    super.initState();
    try {
      _battery.batteryState.then(_updateBatteryState);
      _batteryStateSubscription =
          _battery.onBatteryStateChanged.listen(_updateBatteryState);
    } catch (_) {
      // flutter build mode, catch the exception
    }
  }

@koji-1009
Copy link
Contributor Author

Tested on local server, no problems. Therefore, I believe the problem is caused by hosting on GitHub Pages.

https://stackoverflow.com/questions/65536213/how-to-open-compiled-flutter-web-index-html-in-browser-locally

I'll update the code base and update the demo. And after adding comments (just in case), I will open the PR!

@koji-1009
Copy link
Contributor Author

demo: https://koji-1009.github.io/plus_plugins/
pr: koji-1009#5

@koji-1009 koji-1009 marked this pull request as ready for review March 19, 2024 14:37
@koji-1009
Copy link
Contributor Author

Another option is to add a method that determines the browser and returns true only for chrome or edge.
This would be different from other plugin implementations, but it seems like it would avoid Exception, so it may be worth considering.

@miquelbeltran
Copy link
Member

Thank you Koji! This is already a big leap forward. We are going to be merging it for now, and our plan is to release all packages by Thursday, we are just waiting for another PR to land.

@miquelbeltran miquelbeltran merged commit 21ccfa4 into fluttercommunity:main Mar 19, 2024
19 checks passed
@koji-1009 koji-1009 deleted the feat/battery_plus_web branch March 19, 2024 22:02
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.

None yet

3 participants