flutter_workmanager
flutter_workmanager copied to clipboard
Fetching data from GetStorage inside worker wont work. Always return null value
- [x] I have read the README
- [x] I have done the setup for Android
- [x] I have done the setup for iOS
- [x] I have ran the sample app and it does not work there
Version
| Technology both android and ios | Version 2.17.1 | | Workmanager version | 0.5.0 | | Xcode version | 13.2.1 | | iOS deployment target| 11.0 |
Describe the error fetching the data from getStorage always return the value as null when fetching the value from GetStorage inside worker task
Please provide a example project.
here is example:
void main() async { WidgetsFlutterBinding.ensureInitialized(); await GetStorage.init(); await Workmanager().initialize( callbackDispatcher, isInDebugMode: true ); final GetStorage storage = GetStorage(); storage.write('test', 'hello world'); runApp(MyAppScreen()) }
MyAppScreen extends StatelessWidget{
}
void callbackDispatcher() async { if (Platform.isAndroid) PathProviderAndroid.registerWith(); if (Platform.isIOS) PathProviderIOS.registerWith(); Workmanager().executeTask((task, inputData) async { final GetStorage storage = GetStorage(); print('value>>. '+ storage.read('test')); /// print null always
return Future.value(true);
});
}
when printing the value insider worker task prints null always.
hi @ened any update on this issue?
you need to init getstorage in callbackDispatcher function as below. I think it should work.
void callbackDispatcher() async {
if (Platform.isAndroid) PathProviderAndroid.registerWith();
if (Platform.isIOS) PathProviderIOS.registerWith();
Workmanager().executeTask((task, inputData) async {
await GetStorage.init();
final GetStorage storage = GetStorage();
print('value>>. '+ storage.read('test')); /// print null always
return Future.value(true);
});
}
you need to init getstorage in callbackDispatcher function as below. I think it should work.
void callbackDispatcher() async { if (Platform.isAndroid) PathProviderAndroid.registerWith(); if (Platform.isIOS) PathProviderIOS.registerWith(); Workmanager().executeTask((task, inputData) async { await GetStorage.init(); final GetStorage storage = GetStorage(); print('value>>. '+ storage.read('test')); /// print null always return Future.value(true); }); }
Tested, not working on iOS :\ Any suggestion?
Hey guys, Why don't you make a separate class for that thing and call it inside callbackDispatcher? Did you use await before registering it?
await Workmanager().registerPeriodicTask(
"checkDevice",
periodicTask,
initialDelay: const Duration(minutes: 3),
frequency: const Duration(minutes: 15),
constraints: Constraints(networkType: NetworkType.connected),
existingWorkPolicy: ExistingWorkPolicy.keep,
backoffPolicy: BackoffPolicy.exponential,
backoffPolicyDelay: const Duration(seconds: 10),
);
Why do you have to redeclare everything inside callbackDispatcher?
Please check the logs and provide them here as well. Without debug info, it's impossible to get community support.