KenAragorn / create_flutter_provider_app

A boilerplate project created in Flutter using Provider and Firebase.
MIT License
400 stars 117 forks source link

SharedPreferences / not loading on app initialization #17

Closed arturjnt closed 2 years ago

arturjnt commented 2 years ago

The issue is when I close and reopen my app, the color theme and language is not set according to my settings, it's always the default.

In lib/my_app.dart lines 40

locale: languageProviderRef.appLocale,

and 73

themeMode: themeProviderRef.isDarkModeOn

are the issue.

As these are async methods, from lib/providers/language_provider.dart and lib/providers/theme_provider.dart respectively, when the app initially runs, the information isn't there yet.

Let me know if anyone has thoughts how to fix it.

arturjnt commented 2 years ago

I resolved this by surrounding the AuthWidgetBuilder widget in the lib/my_app.dart file with

FutureBuilder(
  future: SharedPreferenceHelper.initSharedPrefs(),
  builder: (_, __) => AuthWidgetBuilder(

And adding a static method (below) to the lib/caches/sharedpref/shared_preference_helper.dart

static Future<void> initSharedPrefs() async {
  await SharedPreferences.getInstance();
}

This way, the app start waits for the shared prefs module to be loaded.