3

我遇到这个错误已经有一段时间了,我想我需要专家的第二眼来解决它,而且我对这种语言真的很陌生^^。

我在 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(...)
 }
}

该应用程序正确地生成了它需要导入的每种语言的所有生成文件,我认为它看起来很简单,当我调试它时,它正在正确获取语言环境。有谁知道为什么会发生这种情况?在此先感谢:祈祷:

4

2 回答 2

3

修复:我只需要添加到从文件localizationsDelegates自动生成 AppStrings.delegate,的文件import 'generated/l10n.dart';中,而不是创建一个新的AppLocalizations.delegate. 像这样:

localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        AppStrings.delegate,
      ],

并完全删除我所做的 AppLocationzations 类,它运行顺利!:)

PD:我添加了这个新库flutter_localized_locales

于 2020-11-13T09:54:11.570 回答
0

我使用Android Studio Flutter,Intl plugin问题是一样的

main.dart添加导入import 'generated/l10n.dart';

还添加 S.delegate本地化代理列表

localizationsDelegates: [
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
    S.delegate,
  ],
于 2021-04-07T22:33:08.870 回答