title: How to keep language changed even after my app is closed in flutter
body: <p>I am using <code>easy_locatizations</code> package for changing my language. Whenever I am changing language in my app, it works only before i close and restart my app. In that case, language is set back to english. I am using <code>provider</code> for state management and <code>shared_preferences</code> for saving my language code and retrieve it when my app starts.</p>
  <p>But there is a problem. Whenever I use <code>notifylisteners()</code> in a specific function <code>getLocale()</code> (see the code below) and then call it inside initState or anywhere else, the function is called infinite times and the screen never loads. I will explain it in detail below.</p>
  <p>Inside my main function -</p>
  <pre><code>runApp(
      ChangeNotifierProvider(
        create: (_) =&gt; AppLocale(),
        child: MyApp(),
      )
    );
  </code></pre>
  <p>MyApp -</p>
  <pre><code>Widget build(BuildContext context) {
      /*AppLocale locale = Provider.of&lt;AppLocale&gt;(context);
      locale.getLocale();*/
      return Consumer&lt;AppLocale&gt;(
        builder: (buildContext, locale, child) {
          /*locale = Provider.of&lt;AppLocale&gt;(context);
          locale.getLocale();*/
          print(&quot;The country code is - ${locale.locale.countryCode}&quot;);
          return ChangeNotifierProvider(
            create: (context) =&gt; APIData(),
            child: MaterialApp(
              title: 'Board Plus',
              theme: ThemeData(
                fontFamily: 'Nunito Sans',
                brightness: Brightness.dark,
                textTheme: TextTheme(
                  headline1: headingStyle1,
                  headline2: headingStyle2,
                  headline3: headingStyle3,
                  headline4: headingStyle4,
                  headline6: bodyText3,
                  bodyText1: bodyText1,
                  bodyText2: bodyText2,
                  button: buttonTextStyle,
                  caption: linkStyle,
                ),
              ),
              //themeMode: _themeMode,
              supportedLocales: L10n.all,
              locale: locale.locale,
              localizationsDelegates: //AppLocalizations.localizationsDelegates,
              [
                AppLocalizations.delegate,
                GlobalMaterialLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate
              ],
              // home: SplashScreen(),
              initialRoute: isViewed==0 || isViewed==null ? 'splash' : 'home',//'language',
              navigatorObservers: &lt;NavigatorObserver&gt;[observer],
              routes: {
                'home': (context) =&gt; AuthenticationWrapper(),
                'splash' : (context) =&gt; SplashScreen(),
                'language' : (context) =&gt; LanguageSelectionScreen(val: 0,),
              },
              scaffoldMessengerKey: scaffoldMessengerKey,
              navigatorKey: navigatorKey,
            ),
          );
        },
      );
    }
  </code></pre>
  <p>Here is my app_locale.dart -</p>
  <pre><code>import 'package:flutter/material.dart';
  import 'package:shared_preferences/shared_preferences.dart';
  
  class AppLocale extends ChangeNotifier {
    Locale? _locale;
  
    set locale(Locale value) {
      _locale = value;
    }
  
    Locale get locale =&gt; _locale ?? Locale('en');
  
    /*getCode() async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      print(&quot;Shared pref initiated&quot;);
      String code = prefs.getString(&quot;code&quot;)??&quot;en&quot;;
      return code;
    }*/
  
     getLocale() async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      print(&quot;Shared pref initiated&quot;);
      String code = prefs.getString(&quot;code&quot;)??&quot;en&quot;;
      print(&quot;Received code - $code&quot;);
      Locale newLocale = Locale(code);
      if(newLocale == Locale('bn')) {
        _locale = Locale('bn');
      } else if(newLocale==Locale('gu')){
        _locale = Locale('gu');
      } else if(newLocale==Locale('en')){
        _locale = Locale('en');
      } else if(newLocale==Locale('pa')){
        _locale = Locale('pa');
      }
      notifyListeners();
      print(&quot;Finally code is - ${_locale?.countryCode}&quot;);
    }
  
    void changeLocale(Locale newLocale, String code) async {
      if(newLocale == Locale('bn')) {
        _locale = Locale('bn');
      } else if(newLocale==Locale('gu')){
        _locale = Locale('gu');
      } else if(newLocale==Locale('en')){
        _locale = Locale('en');
      } else if(newLocale==Locale('pa')){
        _locale = Locale('pa');
      }
  
      SharedPreferences prefs = await SharedPreferences.getInstance();
      await prefs.setString(&quot;code&quot;, code);
      print(&quot;Saved code is  - ${prefs.getString(&quot;code&quot;)}&quot;);
      notifyListeners();
    }
  }
  </code></pre>
  <p>As you can see in this file, i have defined two functions, I am using chageLocale in a page to change my language, which is working fine. But whenever I use <code>getLocale()</code>, I see those print statement inside the function <code>getLocale</code> being printed infinitely in my console, which mean for whatever reason, this function is executing infinitely. And this problem only exists if I use changeNotifier().</p>
  <p>Now, you can see in MyApp, the commented part is where I called the function and got the result.</p>
  <p>In one of my page <code>ViewProfiles()</code> (one of the initial screens in MyApp), I tried to call the function inside my <code>initState</code> and same thing occured and my page never loaded because of this. And if I don't use <code>notifyListeners()</code>, my language doesn't change after closing and restarting the app.</p>
  <pre><code>   @override
    void initState() {
      profileList = fetchProfileList();
      getToken();
      selected_id = -1;
      Future.delayed(Duration.zero, () {
        AppLocale locale = Provider.of&lt;AppLocale&gt;(context);
        locale.getLocale();
      });
      super.initState();
    }
  </code></pre>