Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter : Strange behavior of Shared Preferences

I have a problem with inconsistent shared preferences value. I will try to describe it as simple as possible.

I'm using Firebase Cloud Messaging for push notifications. When app is in background and notification came in, background handler bellow is invoked.

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  final SharedPreferences prefs = await SharedPreferences.getInstance();
  final int counter = (prefs.getInt('badge') ?? 0) + 1;

  prefs.setInt('badge', counter).then((bool success) {
    print(counter);
  });
}

My widget uses WidgetsBindingObserver to determine lifecycle state. When I enter the app, state of that widget is onResume and there I want to read that badge value from shared preferences like this.

void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      final SharedPreferences prefs = await SharedPreferences.getInstance();
      final int counter = (prefs.getInt('badge') ?? 0); 
      print(counter);
    }
  }

Scenario 1:

  • App opened, notification came in - set badge field to 1.
  • App in background, notification came in - background handler set badge field to 2.
  • App resumed, read that badge field, it's still 1.

Scenario 2:

  • App opened, notification came in - set badge field to 1.
  • App in background, notification came in - background handler set badge field to 2.
  • App in background, notification came in - background handler set badge field to 3.
  • App resumed, read that badge field, it's still 1.

Question: Any idea why field isn't updated?

like image 480
ares4tech Avatar asked Nov 23 '20 01:11

ares4tech


People also ask

What does shared preferences do in Flutter?

shared_preferences is a Flutter plugin that allows you to save data in a key-value format so you can easily retrieve it later. Behind the scenes, it uses the aptly named SharedPreferences on Android and the similar UserDefaults on iOS.

How do I keep logged in using shared preferences Flutter?

Let start on Project make a flutter project and add dependence and after that, you can clear your main dart file source code. In your main. dart file we create a login page UI and here can entry the user details and on button click store the data in share preferences. so follow full source code on these main.

Can we store object in SharedPreferences Flutter?

SharedPreferences: Save and Read First, in order to use the SharedPreferences , we have to use Flutter's plugin for it. To do so, make sure dependencies in your pubspec. yaml file looks similar to this: To save something in SharedPreferences , we must provide a value that we want to save and a key that identifies it.


1 Answers

SharedPreferences can be used on background events handlers. The problem is that the background handler run in a different isolate so, when you try to get a data, the shared preferences instance is empty. To avoid this you simply have to force a refresh:

SharedPreferences prefs= await SharedPreferences.getInstance();
await prefs.reload();
final int counter = (prefs.getInt('badge') ?? 0);

In the same mode, if the shared preferences can be modified in a background hadler, be sure you call this "reload" function in the main isolate when you try to read from theirs.

like image 59
David B.F. Avatar answered Oct 10 '22 13:10

David B.F.