title: Flutter : Strange behavior of Shared Preferences
body: <p>I have a problem with inconsistent shared preferences value. I will try to describe it as simple as possible.</p>
  <p>I'm using Firebase Cloud Messaging for push notifications. When app is in background and notification came in, background handler bellow is invoked.</p>
  <pre><code>Future&lt;void&gt; _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);
    });
  }
  </code></pre>
  <p>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.</p>
  <pre><code>void didChangeAppLifecycleState(AppLifecycleState state) {
      if (state == AppLifecycleState.resumed) {
        final SharedPreferences prefs = await SharedPreferences.getInstance();
        final int counter = (prefs.getInt('badge') ?? 0); 
        print(counter);
      }
    }
  
  </code></pre>
  <p>Scenario 1:</p>
  <ul>
  <li>App opened, notification came in - set badge field to 1.</li>
  <li>App in background, notification came in - background handler set badge field to 2.</li>
  <li>App resumed, read that badge field, it's still 1.</li>
  </ul>
  <p>Scenario 2:</p>
  <ul>
  <li>App opened, notification came in - set badge field to 1.</li>
  <li>App in background, notification came in - background handler set badge field to 2.</li>
  <li>App in background, notification came in - background handler set badge field to 3.</li>
  <li>App resumed, read that badge field, it's still 1.</li>
  </ul>
  <p>Question: Any idea why field isn't updated?</p>