title: Display new shared preferences value every time page is loaded in Flutter?
body: <p>I have a mobile game which saves max score value after the game is finished and in the main page I display that max score. Because I load shared preferences data in main page <code>initState()</code> function, unless user closes and reopens the app the max score does not change on the main page. Any solution ideas ?</p>
  
  <p>this is play_game.dart file where I save results to sharedpreferences when user finishes the game:</p>
  
  <pre><code>  Future saveResults() async {
      SharedPreferences preferences = await SharedPreferences.getInstance();
      int maxSoz = int.parse(preferences.getString('max_soz'));
      int maxPoint = int.parse(preferences.getString('max_xal'));
      if (speed &gt; maxPoint &amp;&amp; soz &gt; maxSoz) {
        preferences.setString('max_xal', speed.toString());
        preferences.setString('max_soz', soz.toString());
      }
    }
  </code></pre>
  
  <p>and this is the landing_page.dart where I display the max scores.
  I have a function to load sharedpreferences data:</p>
  
  <pre><code>  Future loadData() async {
      SharedPreferences preferences = await SharedPreferences.getInstance();
      setState(() {
        this.maxSoz = preferences.getString('max_soz') ?? '0';
        this.maxPoint = preferences.getString('max_xal') ?? '0';
      });
    }
  </code></pre>
  
  <p>and to load data, I mention that function in initState():</p>
  
  <pre><code>  @override
    void initState() {
      super.initState();
      loadData();
    }
  </code></pre>
  
  <p><code>initState()</code> loades data (executes <code>loadData()</code> function) when page is initialized first time. If gamer, after finishing the game, comes back to landing page from play_game page by clicking to "go back" button, the new max scores are not being shown on landing page because, as you may guess, function <code>loadData()</code> only gets executed when page initalized (by <code>initState()</code>).</p>