gpt4 book ai didi

flutter : The getter 'languageCode' was called on null

转载 作者:行者123 更新时间:2023-12-02 10:11:48 29 4
gpt4 key购买 nike

我正在尝试在我的应用程序中实现本地化。
我的应用程序仅支持英语和印地语两种语言。但用户可以选择从设备设置中选择英语或印地语以外的任何语言。如果用户选择了任何其他语言,我想将应用程序区域设置设置为印地语。为此,我使用 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 被调用了两次,第一次区域设置的值为空,第二次它给出了一些值。

flutter :区域设置---->null。

flutter :区域设置---->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(),
);
}
}

最佳答案

我有一个多语言支持的工作代码。请将您的代码与此代码进行比较。不过,如果您无法弄清楚,请与我联系以获取完整的源代码。我有一个关于这个东西的小演示项目。

class MyApp extends StatefulWidget {

static void setLocale(BuildContext context, Locale newLocale) {
var state = context.findAncestorStateOfType<_MyAppState>();
state.setLocale(newLocale);
}

@override
State<StatefulWidget> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {


Locale _locale;

void setLocale(Locale locale) {
setState(() {
_locale = locale;
});
}

@override
void didChangeDependencies() async {
getLocale().then((locale) {
setState(() {
_locale = locale;
});
});
super.didChangeDependencies();
}

@override
Widget build(BuildContext context) {
return MaterialApp(
builder: (context, child) {
return MediaQuery(
child: child,
data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
);
},
title: 'Multi Language',
debugShowCheckedModeBanner: false,
locale: _locale,
home: Home(),
supportedLocales: [
Locale('en', ''),
Locale('ar', ''),
Locale('hi', '')
],
localizationsDelegates: [
AppLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
localeResolutionCallback: (locale, supportedLocales) {
for (var supportedLocale in supportedLocales) {
if (supportedLocale?.languageCode == locale?.languageCode &&
supportedLocale?.countryCode == locale?.countryCode) {
return supportedLocale;
}
}
return supportedLocales?.first;
},
);
}
}

关于 flutter : The getter 'languageCode' was called on null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57806909/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com