fix: NullPointerException by replacing companion FlutterLoader with FlutterInjector singleton#672
Open
absar wants to merge 1 commit into
Open
fix: NullPointerException by replacing companion FlutterLoader with FlutterInjector singleton#672absar wants to merge 1 commit into
absar wants to merge 1 commit into
Conversation
…fer FlutterEngine creation to fix NullPointerException on io.flutter.embedding.engine.loader.FlutterApplicationInfo.flutterAssetsDir, when App and workmanager triggers at the same time. Fixes: fluttercommunity#671 Closes: fluttercommunity#671
|
To view this pull requests documentation preview, visit the following URL: docs.page/fluttercommunity/flutter_workmanager~672 Documentation is deployed and generated using docs.page. |
Contributor
Author
|
@ened for your review please |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Replace companion
FlutterLoaderwith FlutterInjector singletonFlutterInjector.instance().flutterLoaderand deferFlutterEnginecreation to fix NullPointerException on io.flutter.embedding.engine.loader.FlutterApplicationInfo.flutterAssetsDir, when App and workmanager triggers at the same time.Change 1: Remove the separate companion FlutterLoader:
The private val flutterLoader = FlutterLoader() in the companion object created a second FlutterLoader instance independent of FlutterInjector.instance().flutterLoader(). This meant the worker initialized one loader while FlutterEngine and the main app used a different one, causing flutterApplicationInfo to be null on the injector's loader when findAppBundlePath() was called. It also caused two separate FlutterJNI.loadLibrary calls, producing the repeated "called more than once" warnings.
The fix uses FlutterInjector.instance().flutterLoader() directly in startWork(), ensuring the worker, the FlutterEngine, and the main app's Activity delegate all operate on the exact same loader instance throughout the process lifetime.
Change 2: Move FlutterEngine creation inside the ensureInitializationCompleteAsync callback:
Previously engine = FlutterEngine(applicationContext) was called before startInitialization, meaning the engine's internal initialization raced with the subsequent loader init. Moving the engine creation inside the callback guarantees the loader is fully initialized before the engine is constructed, eliminating the race entirely.
Fixes: #671
Closes: #671