[x] I have ran the sample app and it does not work there
Version
Technology
Version
Workmanager version
0.5.0
Xcode version
n/a
Swift version
n/a
iOS deployment target
n/a
Describe the error
I tried adding workmanager to my flutter project. Following the docs in pub.dev, I added the initialization code to my main method:
void main() {
Workmanager().initialize(
callbackDispatcher, // The top level function, aka callbackDispatcher
isInDebugMode:
true // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
);
runApp(const MyApp());
}
But I got this error:
FlutterError (Binding has not yet been initialized.
The "instance" getter on the ServicesBinding binding mixin is only available once that binding has been initialized.
Typically, this is done by calling "WidgetsFlutterBinding.ensureInitialized()" or "runApp()" (the latter calls the former). Typically this call is done in the "void main()" method. The "ensureInitialized" method is idempotent; calling it multiple times is not harmful. After calling that method, the "instance" getter will return the binding.
In a test, one can call "TestWidgetsFlutterBinding.ensureInitialized()" as the first line in the test's "main()" method to initialize the binding.
If ServicesBinding is a custom binding mixin, there must also be a custom binding class, like WidgetsFlutterBinding, but that mixes in the selected binding, and that is the class that must be constructed before using the "instance" getter.)
Following the error messages advice, I added the following before the initialize call:
WidgetsFlutterBinding.ensureInitialized();
And it worked! I noticed the sample app calls Workmanager.initialize() in a button callback, but when I moved it to main() the same thing happened and the same fix worked. Should the documentation be updated?
Output of flutter doctor -v
[√] Flutter (Channel stable, 3.3.0, on Microsoft Windows [Version 10.0.19044.2006], locale
en-US)
• Flutter version 3.3.0 on channel stable at C:\tools\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision ffccd96b62 (5 weeks ago), 2022-08-29 17:28:57 -0700
• Engine revision 5e9e0e0aa8
• Dart version 2.18.0
• DevTools version 2.15.0
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at C:\Users\gkado\AppData\Local\Android\sdk
X cmdline-tools component is missing
Run path/to/sdkmanager --install "cmdline-tools;latest"
See https://developer.android.com/studio/command-line for more details.
X Android license status unknown.
Run flutter doctor --android-licenses to accept the SDK licenses.
See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.
[√] Chrome - develop for the web
• Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
[√] Visual Studio - develop for Windows (Visual Studio Professional 2019 16.11.8)
• Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional
• Visual Studio Professional 2019 version 16.11.32002.261
• Windows 10 SDK version 10.0.19041.0
Version
Describe the error I tried adding workmanager to my flutter project. Following the docs in pub.dev, I added the initialization code to my main method:
But I got this error:
Following the error messages advice, I added the following before the initialize call:
And it worked! I noticed the sample app calls Workmanager.initialize() in a button callback, but when I moved it to main() the same thing happened and the same fix worked. Should the documentation be updated?
Output of
flutter doctor -v
[√] Flutter (Channel stable, 3.3.0, on Microsoft Windows [Version 10.0.19044.2006], locale en-US) • Flutter version 3.3.0 on channel stable at C:\tools\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision ffccd96b62 (5 weeks ago), 2022-08-29 17:28:57 -0700 • Engine revision 5e9e0e0aa8 • Dart version 2.18.0 • DevTools version 2.15.0[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at C:\Users\gkado\AppData\Local\Android\sdk X cmdline-tools component is missing Run
path/to/sdkmanager --install "cmdline-tools;latest"
See https://developer.android.com/studio/command-line for more details. X Android license status unknown. Runflutter doctor --android-licenses
to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.[√] Chrome - develop for the web • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
[√] Visual Studio - develop for Windows (Visual Studio Professional 2019 16.11.8) • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional • Visual Studio Professional 2019 version 16.11.32002.261 • Windows 10 SDK version 10.0.19041.0
[√] Android Studio (version 2021.1) • Android Studio at C:\Program Files\Android\Android Studio • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822)
[√] VS Code (version 1.71.2) • VS Code at C:\Users\gkado\AppData\Local\Programs\Microsoft VS Code • Flutter extension version 3.48.0
[√] Connected device (4 available) • Pixel 3a (mobile) • 97SAY11YWN • android-arm64 • Android 12 (API 32) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19044.2006] • Chrome (web) • chrome • web-javascript • Google Chrome 105.0.5195.127 • Edge (web) • edge • web-javascript • Microsoft Edge 99.0.1150.36
[√] HTTP Host Availability • All required HTTP hosts are available