Flutter Localization is not stable after after kill or destroy app

Issue

I have used the Get package for localization and it’s working fine. but if I destroy(kill, close) the app and open again so that time by default local language is set.

Example:- Open app-> language(English)-> change language(arabic)->it working -> close(destroy, kill) app-> again open the -> by default set language(English).

above is the flow of the process.

  GetMaterialApp(
        locale: Get.locale,
        translations: LLLanguages(),
        navigatorKey:Get.key,
        debugShowCheckedModeBanner:false,
        theme:LLTheme.light,
        darkTheme: LLTheme.dark,
        themeMode: ThemeService().theme,
        home:LLSplashScreen(),
      )



Get.updateLocale(Locale('ar','DZ'));

Thanks in advance

Solution

It’s the expected behavior. You need to save your active localization/language flag in persistent storage like GetStorage, SharedPreferences, or Hive. And you need to get that value and set localization according to that value on app start.

main() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();

    var isArabic= prefs.getBool('isArabic') ?? false;

   runApp(MyApp(isArabic));
}


class MyApp extends StatelessWidget{
    final bool isArabic;

    MyApp(this.isArabic);

    Widget build()=>   GetMaterialApp(
    locale: isArabic?Locale('ar','DZ'): Locale('en','US'),
    translations: LLLanguages(),
    navigatorKey:Get.key,
    debugShowCheckedModeBanner:false,
    theme:LLTheme.light,
    darkTheme: LLTheme.dark,
    themeMode: ThemeService().theme,
    home:LLSplashScreen(),
  );
 }

Answered By – S. M. JAHANGIR

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *