【问题标题】:Flutter : The getter 'languageCode' was called on nullFlutter:在 null 上调用了 getter 'languageCode'
【发布时间】:2020-01-08 10:34:04
【问题描述】:

我正在尝试对我的应用实施本地化。
我的应用程序仅支持英语和印地语两种语言。但用户可以选择从除英语或印地语以外的设备设置中选择任何语言。如果用户选择了任何其他语言,我想将应用程序区域设置为印地语。为此,我使用localeResolutionCallback,如下面的代码所示。但我收到了下面提到的错误消息。

flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following NoSuchMethodError was thrown building ScrollConfiguration(behavior:
flutter: _MaterialScrollBehavior):
flutter: The getter 'languageCode' was called on null.
flutter: Receiver: null
flutter: Tried calling: languageCode
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
flutter: #1      Demo.build.<anonymous closure> (package:simple/main.dart:49:54)

调试后我发现localeResolutionCallback被调用了两次,第一次locale的值为null,第二次给出了一些值。

flutter: locale---->null .

flutter: locale---->en_US .

  1. 为什么 localeResolutionCallback 会被调用两次,但值为空
  2. 这是将设备默认语言环境覆盖为应用特定语言环境的正确方法吗
class Demo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      onGenerateTitle: (BuildContext context) => AppLocalizations.of(context).title,
      localizationsDelegates: [
        AppLocalizationsDelegate(),
        GlobalWidgetsLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: [
        const Locale('en', ''), // English
        const Locale('hi', ''), // hindi
      ],
      localeResolutionCallback: (locale, supportedLocales) {
        for (var supportedLocale in supportedLocales) {
          if (supportedLocale.languageCode == locale.languageCode &&
              supportedLocale.countryCode == locale.countryCode) {
            return supportedLocale;
          }
        }
        return supportedLocales.last;
      },
      home: DemoApp(),
    );
  }
}

【问题讨论】:

  • 你找到解决办法了吗??
  • 你找到更好的解决方案了吗,我也遇到了同样的问题
  • 如果您有足够的信心确保您的代码完美无误,那么只需卸载应用程序、flutter clean 和 pub get、使缓存无效并重新启动 android studio。之后运行您的应用程序,它应该不会显示此错误。

标签: flutter localization


【解决方案1】:

我找到了解决方案,但这只会强制您的应用使用默认语言

for (var supportedLocale in supportedLocales) {
      if (locale!=null && locale.languageCode!=null && locale.countryCode!=null && supportedLocale.languageCode == locale.languageCode &&
          supportedLocale.countryCode == locale.countryCode){
        return supportedLocale;
      }
    }
    // If the locale of the device is not supported, or locale returns null
    // use the last one from the list (Hindi, in your case).
    return supportedLocales.last;

我会尽快找到更好的解决方案,我会通知您。使用 android 设备 locale 返回默认手机语言,但在 iOs 中出现此错误。

【讨论】:

  • 你找到更好的解决方案了吗,我也遇到了同样的问题?
【解决方案2】:

如果未检测到语言环境,则发送 null 是好的,但不好的是接收第一个 null 并在一毫秒后接收正确的语言。我认为检测有问题,它会在应用初始化之前尝试检测语言环境?

所以这段代码运行良好,只是它首先使用默认语言,然后使用正确的语言环境。

localeResolutionCallback (Locale locale, Iterable<Locale> supportedLocales) {
  if (locale == null) {
    debugPrint("*language locale is null!!!");
    return supportedLocales.first;
  }

  for (Locale supportedLocale in supportedLocales) {
    if (supportedLocale.languageCode == locale.languageCode ||
        supportedLocale.countryCode == locale.countryCode) {
      debugPrint("*language ok $supportedLocale");
      return supportedLocale;
    }
  }

  debugPrint("*language to fallback ${supportedLocales.first}");
  return supportedLocales.first;
}

这个答案来自:gimox

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-11
    • 2021-12-10
    • 2020-03-24
    • 2021-05-25
    • 2023-03-08
    • 2019-12-12
    • 2020-08-01
    相关资源
    最近更新 更多