我遇到这个错误已经有一段时间了,我想我需要专家的第二眼来解决它,而且我对这种语言真的很陌生^^。
我在 Flutter 中将 localizable 添加到我的项目中,以不同的语言添加了所有 files.arb 并尝试按照 Google 的教程和其他不同的解决方法导入它,但仍然收到相同的错误:
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building StyleguideScreen(dirty):
The getter 'welcomeGeneralInfoTitle' was called on null.
Receiver: null
Tried calling: welcomeGeneralInfoTitle
这是我用于 localicationDelegates 的 AppLocalizations.dart 类
class AppLocalizations {
static const AppLocalizationsDelegate delegate = AppLocalizationsDelegate();
static Future<AppLocalizations> load(Locale locale) {
final String name = locale.countryCode.isEmpty ? locale.languageCode : locale.toString();
final String localeName = Intl.canonicalizedLocale(name);
return initializeMessages(localeName).then((_) {
Intl.defaultLocale = localeName;
return AppLocalizations();
});
}
static AppLocalizations of(BuildContext context) {
return Localizations.of<AppLocalizations>(context, AppLocalizations);
}
String get welcomeGeneralInfoTitle {
return Intl.message('Bet Master', name: 'title', desc: 'App Title');
}
}
class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
const AppLocalizationsDelegate();
List<Locale> get supportedLocales {
return const <Locale>[
Locale('en', ''),
Locale('de', ''),
Locale('es', ''),
Locale('es', 'ES'),
]; //Still need to add 18 languages, is there a better way to add them?
}
@override
bool isSupported(Locale locale) => _isSupported(locale);
@override
Future<AppLocalizations> load(Locale locale) => AppLocalizations.load(locale);
@override
bool shouldReload(AppLocalizationsDelegate old) => false;
bool _isSupported(Locale locale) {
if (locale != null) {
for (Locale supportedLocale in supportedLocales) {
if (supportedLocale.languageCode == locale.languageCode) {
return true;
}
}
}
return false;
}
}
这是我添加到项目根目录的地方
return MaterialApp(
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: AppLocalizations.delegate.supportedLocales,
title: 'AMP',
theme: Theme.darkTheme,
home: StyleguideScreen(),
);
这是我尝试实现它的方法,它崩溃了
class StyleguideScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final welcome = AppStrings.current.welcomeGeneralInfoTitle;
return Scaffold(...)
}
}
该应用程序正确地生成了它需要导入的每种语言的所有生成文件,我认为它看起来很简单,当我调试它时,它正在正确获取语言环境。有谁知道为什么会发生这种情况?在此先感谢:祈祷: