flutter_workmanager icon indicating copy to clipboard operation
flutter_workmanager copied to clipboard

Fetching data from GetStorage inside worker wont work. Always return null value

Open neeluagrawal04 opened this issue 3 years ago • 7 comments

  • [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

neeluagrawal04 avatar May 17 '22 08:05 neeluagrawal04

Please provide a example project.

ened avatar May 17 '22 12:05 ened

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.

neeluagrawal04 avatar May 18 '22 04:05 neeluagrawal04

hi @ened any update on this issue?

neeluagrawal04 avatar May 19 '22 16:05 neeluagrawal04

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);
});
}

alperenbaskaya58 avatar Jul 02 '22 12:07 alperenbaskaya58

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?

scottthoo avatar Jul 16 '22 14:07 scottthoo

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?

gOzaru avatar Aug 01 '22 07:08 gOzaru

Please check the logs and provide them here as well. Without debug info, it's impossible to get community support.

ened avatar Aug 06 '22 09:08 ened