title: How do I initialize a long-lived class in a Flutter app?
body: <p>I have an <code>Api</code> class that accepts an (optional) authentication <code>token</code> that it uses for making authenticated requests e.g. <code>Api(token: 'a9sa2ksas12').getUserDetails()</code>.</p>
  
  <p>If the token is not passed, it has to perform the relatively expensive operation of reading it from <code>sharedPreferences</code>.</p>
  
  <pre><code>class Api {
    static const BASEURL = "https://api.google.com/";
    final String token;
  
    Api({ this.token });
  
    Future&lt;http.response&gt; getUserDetails() async {
      return http.get('$BASEURL/user/', headers: { 'Authorization': token });
    }
  }
  </code></pre>
  
  <p>How can I setup my app so that the token is read only once from <code>sharedPreferences</code> and used throughout the app for all future <code>Api()</code> requests?</p>
  
  <p>Some ideas I've considered and think that may work:</p>
  
  <ul>
  <li>Have <code>token</code> be a global variable</li>
  <li>Make the API class a singleton, and pass it around between "screens"</li>
  </ul>