feat(connectivity): improve network callback unregistration handling #3681
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Description
fix: safely unregister Android network callback to prevent crash when callback already unregistered or null
Problem
The app crashes with
IllegalArgumentException: NetworkCallback was not registered
when the Flutter engine is destroyed, particularly during background work with WorkManager. This happens because Android'sConnectivityManager
checks ifnetworkCallback.networkRequest
is null before unregistering:Since
networkRequest
is an internal field that we cannot check directly, and it can become null in various scenarios (double cleanup, process recreation, etc.), we need to handle the unregistration defensively.Related Issues
#1025
Checklist
- [x] I read the [Contributor Guide](https://github.com/fluttercommunity/plus_plugins/blob/main/CONTRIBUTING.md) and followed the process outlined there for submitting PRs.
- [x] I titled the PR using Conventional Commits.
- [x] I did not modify the
CHANGELOG.md
nor the plugin version inpubspec.yaml
files.- [x] All existing and new tests are passing.
- [x] The analyzer (
flutter analyze
) does not report any problems on my PR.Breaking Change
Does this PR require plugin users to manually update their apps?
- [ ] Yes, this is a breaking change (add
!
in the title).- [x] No, this is *not* a breaking change.