jonataslaw / get_storage

A fast, extra light and synchronous key-value storage to Get framework
MIT License
369 stars 83 forks source link

Getting null when get storage is running in background #68

Open ASVKVINAYAK opened 3 years ago

ASVKVINAYAK commented 3 years ago

I am using workmanager plugin to run my app in the background and using getstorage for saving user information but when the app runs in the background get storage returns null for the particular read data What should I do I am not getting what is wrong in my code Any suggestions are welcome

ad-on-is commented 3 years ago

I have exactly the same problem. Specifically, when using get_storage inside FCMs FirebaseMessaging.onBackgroundMessage.

zeroprofit commented 3 years ago

You need to explicitly declare a variable to be nullable. For example,

bool isLoggedIn = GetStorage().read('isLoggedIn') ?? false; // This will fail

vs

bool? isLoggedIn = GetStorage().read('isLoggedIn') ?? false; // This will work because "bool?" declare "isLoggedIn" nullable

ajipandean commented 3 years ago

Mine too. My get_storage got reset when receiving message from FirebaseMessaging.onBackgroundMessage.

Is there anyone got the solution for this?

ajipandean commented 3 years ago

I ended up writing two GetStorage.init() both in main() and FirebaseMessaging.onBackgroundMessage handler and it works for me.

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await GetStorage.init(); // here
  await $.firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(handleOnBackgroundMessage);
  runApp(HospitalApp());
}

Future<void> handleOnBackgroundMessage(RemoteMessage event) async {
  await GetStorage.init(); // and here
  await $.firebase.initializeApp();
  $.firebase.handleOnMessage(event);
}
torphix commented 3 years ago

Are you able to save data in the handleOnBackgroundMessage function? As i am unable to save data when receiving notifications in the background.

ajipandean commented 3 years ago

@henry8th Yeah sure, I can. I have no issue for storing data in FirebaseMessaging.onBackgroundMessage handler.

diegosiao commented 2 years ago

Same problem here, what Flutter version are you using @ajipandean?

ajipandean commented 2 years ago

My flutter version is Flutter v2.2.3. @diegosiao

adnan-jamil commented 2 years ago

Hi, there the Getstorage is not working in this Scenario

neeluagrawal04 commented 2 years ago

not working at my end as well when using GetStorage in workmanager background process. It always returns null. Any solution for it?

zhicheng-Fu commented 2 years ago

@ajipandean
any solution for this one? add GetStorage.init(); but got error

 No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider
neeluagrawal04 commented 2 years ago

@ajipandean any solution for this one? add GetStorage.init(); but got error

 No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider

I am also getting same exception.

emman-ok commented 1 year ago

I had the same issue trying to us GetStorage to store data on FCM background message and read the data on app resume, but it did not work. I have to revert to shared_preferences, which suffers the same fate. But shared_preference have reload() function which you can use to sync values saved in background and in foreground.

Expecting GetStorage to implement the same, since flutter has separate memory handler for background and foreground.