简体   繁体   English

请求了不包括本地化祖先的上下文的语言环境

[英]Requested the Locale of a context that does not include a Localizations ancestor

I am trying to get the Locale of the user's phone at app start.我正在尝试在应用程序启动时获取用户手机的Locale

I have this Widget tree in the runApp() method :我在runApp()方法中有这个Widget树:

@override
  Widget build(BuildContext context) {
    return MaterialApp(
            locale: Locale(Localizations.localeOf(context).languageCode), // This crashes
            localizationsDelegates: [
              const LocalizationDelegate(), // My custom delegate to get translations
              CountryLocalizations.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
            ],
            supportedLocales: [
              Locale("en"),
              Locale("fr"),
            ],
            debugShowCheckedModeBanner: false,
            home: Scaffold(
              resizeToAvoidBottomInset: false,
              body: HomePage(),
            )
        );
}

the line locale: Locale(Localizations.localeOf(context).languageCode) cause a crash with :locale: Locale(Localizations.localeOf(context).languageCode)导致崩溃:

Requested the Locale of a context that does not include a Localizations ancestor.

I simply want to bind this locale to my Delegate without going further in app.我只是想将此语言环境绑定到我的Delegate ,而无需在 app.xml 中进一步操作。
So far, i put locale: Locale("en") to read my en.json file from my LocalizationDelegate .到目前为止,我把locale: Locale("en")从我的LocalizationDelegate读取我的en.json文件。

You have to use a MaterialApp or CupertinoApp to use Locales.您必须使用 MaterialApp 或 CupertinoApp 才能使用语言环境。

For ex,例如,

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: const <Locale>[
        Locale('de'),
        Locale('en'),
        Locale('es')
      ],
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const YourHomeWidget(), // All Localized Widgets must be placed inside home widget.
    );
  }
}

NOTE: Flutter Localization automatically detect system language and apply this settings to your application.注意:Flutter Localization 会自动检测系统语言并将此设置应用于您的应用程序。

I also answer because @Jaydipsinh is right but the error i was making is that i thought i needed this line :我也回答是因为@Jaydipsinh 是对的,但我犯的错误是我认为我需要这一行:

locale: Locale(Localizations.localeOf(context).languageCode)

Whereas it's useless.而它是无用的。
You simply have to put supportedLocales values and if they match the phone's locale, flutter automatically use the langage for the given delegates , for example :您只需输入supportedLocales值,如果它们与手机的语言环境匹配,flutter 会自动使用给定delegates的语言,例如:

GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate

Also, you can retrieve the locale by using : String locale = Platform.localeName.split('_')[0];此外,您可以使用以下方法检索语言环境: String locale = Platform.localeName.split('_')[0]; with no crash at app start.应用程序启动时没有崩溃。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用不包含导航器的上下文请求的导航器操作 - Navigator operation requested with a context that does not include a Navigator 使用不包含导航器的上下文请求的导航器操作 - Navigator operation requested with a context that does not include a Navigator Flutter:使用不包含导航器的上下文请求的导航器操作 - Flutter : Navigator operation requested with a context that does not include a Navigator 使用不包含 Navigator 的上下文请求的 Navigator 操作。(Flutter) - Navigator operation requested with a context that does not include a Navigator.(Flutter) 使用不包含导航器的上下文请求导航器操作 - Flutter - Navigator operation requested with a context that does not include a Navigator - Flutter 为什么“使用不包含导航器的上下文请求导航器操作” - WHY "Navigator operation requested with a context that does not include a Navigator" 错误:使用不包含导航器的上下文请求导航器操作 - Error: Navigator operation requested with a context that does not include a Navigator Flutter 小部件测试:使用不包含 AutoRouter 的上下文请求 AutoRouter 操作 - Flutter Widget Test: AutoRouter operation requested with a context that does not include an AutoRouter 使用不包含 Navigator (Flutter) 的上下文请求的 Navigator 操作 - Navigator operation requested with a context that does not include a Navigator (Flutter) 异常:使用不包含导航器的上下文请求的导航器操作 - Exception : Navigator operation requested with a context that does not include a Navigator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM