Skip to content

fix(android): prevent crash if callbackInfo is null in BackgroundWorker (fixes #527) #615

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jonathanduke
Copy link

@jonathanduke jonathanduke commented Jul 9, 2025

Fixes #527

This PR prevents a NullPointerException when FlutterCallbackInformation.lookupCallbackInformation(...) returns null in BackgroundWorker.kt.

Cause

If the stored callback handle (retrieved from SharedPreferences) is invalid or stale, lookupCallbackInformation(...) returns null. The existing code attempts to access callbackInfo.callbackLibraryPath without checking for null, which crashes the app.

Fix

Add a null check for callbackInfo and fail gracefully:

if (callbackInfo == null) {
    Log.e(TAG, "Failed to resolve Dart callback for handle $callbackHandle.")
    completer?.set(Result.failure())
    return@ensureInitializationCompleteAsync
}

This should prevent the app from terminating with an error such as those mentioned in the linked issue:
Attempt to read from field 'java.lang.String io.flutter.view.FlutterCallbackInformation.callbackLibraryPath'

EDIT: fixed compilation error on return thanks to @Tulleb

Copy link

docs-page bot commented Jul 9, 2025

To view this pull requests documentation preview, visit the following URL:

docs.page/fluttercommunity/flutter_workmanager~615

Documentation is deployed and generated using docs.page.

@Tulleb
Copy link

Tulleb commented Jul 17, 2025

I am facing compilation issue with your fix @jonathanduke:

e: file:///[...]/BackgroundWorker.kt:100:17 'return' is prohibited here.
e: file:///[...]/BackgroundWorker.kt:100:24 Return type mismatch: expected 'com.google.common.util.concurrent.ListenableFuture<androidx.work.ListenableWorker.Result>', actual 'androidx.work.ListenableWorker.Result'.

FAILURE: Build failed with an exception.

@jonathanduke
Copy link
Author

I am facing compilation issue with your fix @jonathanduke:

e: file:///[...]/BackgroundWorker.kt:100:17 'return' is prohibited here.
e: file:///[...]/BackgroundWorker.kt:100:24 Return type mismatch: expected 'com.google.common.util.concurrent.ListenableFuture<androidx.work.ListenableWorker.Result>', actual 'androidx.work.ListenableWorker.Result'.

FAILURE: Build failed with an exception.

Thank you, @Tulleb. I was able to update and compile locally.

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.

java.lang.NullPointerException while app is in backeground around 2 hours than it's getting crashed
2 participants