I am getting the default language after restarting my app but i want to get the upadated langauge
`
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return GetMaterialApp(
translations: LocalString(),
locale: const Locale('en', 'US'),
debugShowCheckedModeBanner: false,
),
home: homeScreen();
`
CodePudding user response:
You can use shared preferences to store the locale then retrieve that stored locale value from shared preferences wherever you need.
CodePudding user response:
When an user picks a language, in GetX you can update the locale using the following function:
void changeLang(String lang) {
Locale locale = Locale(lang);
Get.updateLocale(locale);
}
You can use the above function in the following ways:
changeLang('en')
or
changeLang('bn')
So, after the user has changed language you can store it in GetStorage(https://pub.dev/packages/get_storage) and retrieve it later.
After importing the GetStorage package. Add the following line before runApp(const MyApp());
await GetStorage.init();
After initialising getstorage, you can save your locale using the following code:
Future<bool> saveLanguage(String? lang) async {
final box = GetStorage();
await box.write('lang', lang);
return true;
}
To retrieve the saved language, you can use the following code:
String? getLang() {
final box = GetStorage();
return box.read('lang');
}
So, when user re-runs app, check if getLang() is null or not. If it is not null you can use the following code to update your locale accordingly:
if(getLang() != null) {
Locale locale = Locale(getLang()!);
Get.updateLocale(locale);
}
CodePudding user response:
Use shared preferences: https://pub.dev/packages/shared_preferences
In my experience, use a singleton. Example:
class PrefsInstance {
static PrefsInstance _instance = new PrefsInstance.internal();
PrefsInstance.internal();
factory PrefsInstance() => _instance;
Future<void> saveAccessToken(String token) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
print("saveAccessToken");
await prefs.setBool(GeneralPrefsConstant.PREF_KEY_LOGIN, true);
await prefs.setString(GeneralPrefsConstant.PREF_KEY_ACCESS_TOKEN, token);
DataInstance().isLogin = true;
DataInstance().accessToken = token;
}
Future<void> logOut() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
print("LOG OUT -> FIX SHARED PREFERENCES");
await prefs.setBool(GeneralPrefsConstant.PREF_KEY_LOGIN, false);
await prefs.setString(GeneralPrefsConstant.PREF_KEY_ACCESS_TOKEN, "");
await prefs.setString(GeneralPrefsConstant.PREF_KEY_PROFILE, "");
DataInstance().isLogin = false;
DataInstance().accessToken = "";
}
saveLanguage() async {...}
}
The shared preference variables will be saved into your phone's memory and won't disappear when you close the app. Each variable has specific key (GeneralPrefsConstant
). It's every easy to use. If you want to save, use asynchronous method setString(key, value)
. If you want to get, use getString(key)
(not async). If your app was installed the first time, it could be nullable when get shared preference, so it is necessary to check carefully.
Good luck.