title: "Flutter" Fetch data from shared_preferences and provider before running the app
body: <p>I am creating a flutter app, I am using shared_preferences to store the themeMode and using provider to manage state. I am able to fetch the themeMode before my app starts but not able to run the provider function that sets the themeMode.</p>
  <p>Following is my code</p>
  <pre><code>Future&lt;void&gt; main() async {
    WidgetsFlutterBinding.ensureInitialized();
    SharedPreferences.getInstance().then((prefs) {
      var isDarkTheme = prefs.getBool(&quot;isDarkTheme&quot;) ?? false;
      return runApp(
        MultiProvider(
          providers: [
            ChangeNotifierProvider(
              create: (_) =&gt; ThemeManager(),
            ),
          ],
          child: MyApp(isDarkActive: isDarkTheme),
        ),
      );
    });
  }
  </code></pre>
  <p>My Provider &quot;State Management&quot; code</p>
  <pre><code>class ThemeManager with ChangeNotifier {
    bool _isDark = false;
  
    void setThemeMode(bool themeMode) async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      prefs.setBool(&quot;isDarkTheme&quot;, themeMode);
      _isDark = themeMode;
      debugPrint(_isDark.toString());
      notifyListeners();
      // return true;
    }
  
    void fetchTheme() async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      var darkMode = prefs.getBool('isDarkTheme');
      if (darkMode != null) {
        _isDark = darkMode;
      } else {
        _isDark = false;
      }
      notifyListeners();
    }
  
    bool get isDark =&gt; _isDark;
  }
  </code></pre>