Skip to content

🐞PlatformException "You have not properly initialized the Flutter WorkManager Package" on first run #645

@jonathanduke

Description

@jonathanduke

Description

After upgrading to the 0.9.0+2 version of the package, I am getting an exception when registering any task after calling initialize(), but it incorrectly claims that I have not called initialize(). I eventually determined that this only occurs on the first run of the application (or the first run after upgrading to this particular version of the plugin).

Unsuccessful Workarounds

  • I added a delayed timer to wait 60 seconds before calling registerPeriodicTask after calling initialize to rule out any race conditions, but the exception persisted.
  • I tried catching and retrying registerPeriodicTask over a period of time to see if it would succeed after additional attempts, but it failed every time.
  • I tried calling initialize() twice, but it still did not fix the issue.
  • The only "fix" was relaunching the app, so the exception seemed to be limited to the first run only.

Steps to Reproduce

  • In your main() method, call await Workmanager().initialize(yourEntryMethod);
  • On the next line, call await Workmanager().registerPeriodicTask("testTask", "testTask");
  • Uninstall or wipe user data on your existing app, if installed.
  • Rebuild and run. You should see the exception on the first run.
  • Close the app and run again. The exception should not appear.

Error

PlatformException(Exception, java.lang.Exception: You have not properly initialized the Flutter WorkManager Package. You should ensure you have called the 'initialize' function first!, Cause: null, Stacktrace: java.lang.Exception: You have not properly initialized the Flutter WorkManager Package. You should ensure you have called the 'initialize' function first!
	at dev.fluttercommunity.workmanager.WorkmanagerPlugin.registerPeriodicTask(WorkmanagerPlugin.kt:79)
	at dev.fluttercommunity.workmanager.pigeon.WorkmanagerHostApi$Companion.setUp$lambda$9$lambda$8(WorkmanagerApi.g.kt:700)
	at dev.fluttercommunity.workmanager.pigeon.WorkmanagerHostApi$Companion.$r8$lambda$idyf-0kIY8_7C_5YHaSad8xqwzo(Unknown Source:0)
	at dev.fluttercommunity.workmanager.pigeon.WorkmanagerHostApi$Companion$$ExternalSyntheticLambda16.onMessage(D8$$SyntheticClass:0)
	at io.flutter.plugin.common.BasicMessageChannel$IncomingMessageHandler.onMessage(BasicMessageChannel.java:261)
	at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292)
	at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
	at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0)
	at android.os.Handler.handleCallback(Handler.java:883)
	at android.os.Handler.dispatchMessage(Handler.java:100)
	at android.os.Looper.loop(Looper.java:214)
	at android.app.ActivityThread.main(ActivityThread.java:7356)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
, null)
       at WorkmanagerHostApi.registerPeriodicTask(workmanager_api.g.dart:700)
       at WorkmanagerAndroid.registerPeriodicTask(workmanager_android.dart:75)

Solution

This appears to be a bug in WorkmanagerPlugin.kt that updates the handle in SharedPrefs but does not update the local variable upon initialization. I am currently testing a PR to fix that scenario.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions