作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在我的应用程序中实现本地化。
我的应用程序仅支持英语和印地语两种语言。但用户可以选择从设备设置中选择英语或印地语以外的任何语言。如果用户选择了任何其他语言,我想将应用程序区域设置设置为印地语。为此,我使用 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。
localeResolutionCallback
被调用两次且值为空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/
我是一名优秀的程序员,十分优秀!