从Flutter的共享偏好中获取语言 [英] Get language from Shared Prefences in Flutter

查看:51
本文介绍了从Flutter的共享偏好中获取语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将当前语言保存在变量中,以便在其他页面中使用.我正在使用带有flutter_translate的shared_preferences.

I want to save the current language in a variable to use it in an other page. I'm using shared_preferences with flutter_translate.

在我的app.dart中,我有这个:

In my app.dart I have this :

class TranslatePreferences implements ITranslatePreferences
{
  static const String _selectedLocaleKey = 'selected_locale';

  @override
  Future<Locale> getPreferredLocale() async
  {
    final preferences = await SharedPreferences.getInstance();

    if(!preferences.containsKey(_selectedLocaleKey)) return null;

    var locale = preferences.getString(_selectedLocaleKey);
    return localeFromString(locale);

  }


  @override
  Future savePreferredLocale(Locale locale) async
  {
    final preferences = await SharedPreferences.getInstance();

    await preferences.setString(_selectedLocaleKey, localeToString(locale));

  }


}

如果我打印:

print(TranslatePreferences().getPreferredLocale());

我得到了:"Future< Locale>"的实例

如何管理它以获取getPreferredLocale()的值并将其存储在变量中?

How I can manage it to get the value of the getPreferredLocale() and store in a variable ?

谢谢

推荐答案

final Future<String> futureValue = Future.value("Hello !");

打印同步

void main() {
  futureValue.then((value) => print(value)); // Hello !
}

async

void main() async {
  final value = await futureValue;
  print(value); // Hello !
}

这篇关于从Flutter的共享偏好中获取语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆