Skip to content

fix: NullPointerException by replacing companion FlutterLoader with FlutterInjector singleton#672

Open
absar wants to merge 1 commit into
fluttercommunity:mainfrom
absar:npe-issue-flutterLoader
Open

fix: NullPointerException by replacing companion FlutterLoader with FlutterInjector singleton#672
absar wants to merge 1 commit into
fluttercommunity:mainfrom
absar:npe-issue-flutterLoader

Conversation

@absar
Copy link
Copy Markdown
Contributor

@absar absar commented Jun 5, 2026

Replace companion FlutterLoader with FlutterInjector singleton FlutterInjector.instance().flutterLoader and defer FlutterEngine creation 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

…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
@docs-page
Copy link
Copy Markdown

docs-page Bot commented Jun 5, 2026

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.

@absar absar changed the title Replace companion FlutterLoader with FlutterInjector singleton to fix NullPointerException fix: NullPointerException by replacing companion FlutterLoader with FlutterInjector singleton Jun 5, 2026
@absar
Copy link
Copy Markdown
Contributor Author

absar commented Jun 6, 2026

@ened for your review please

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

Labels

None yet

Projects

None yet

1 participant