title: Waiting till function is completed (Flutter)
body: <p>I have a problem with my code: my goal is to get data from sharedprefs and use the stored data afterwards. The problem is that the function where I get the data from sharedprefs is too slow, or in other words the function where I call the function for getting the data is too fast.</p>
  <p>The process should be: in the <code>main.dart</code> I call a function <code>getDataString()</code>, which is in the <code>sharedPreferences.dart</code> file, --&gt; the function <code>getDataString()</code> should get the data from sharedprefs --&gt; the data should be printed.</p>
  <p>With my code I get as result null, but when I print the variable <code>savedData</code> a second time, I get the result I'm looking for. So I think that the getDataString function is simply too slow. How can I wait till the function is finished?</p>
  <p><strong>This is the code I tried:</strong></p>
  <p>This is the <code>getDataString()</code> function from the <code>sharedPreferences.dart</code> file:</p>
  <pre><code>Future&lt;String&gt; getDataString() async {
    final prefs = await SharedPreferences.getInstance();
    savedData = jsonDecode(prefs.getString(&quot;savedData&quot;));
  }
  </code></pre>
  <p>This is the <code>initState</code> from the <code>main.dart</code>:</p>
  <pre><code>  void initState() {
      super.initState();
      getData();
      log(savedData.toString());
      });
    }
  </code></pre>