3

I've been having this error for a while and I think I need some second eyes of an expert to solve it and I'm really new in this language ^^.

I added localizable to my project in Flutter, added all the files.arb in different languages and tries to import it following Google's tutorial and other just different work around but keep getting the same error:

    ════════ 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

This is my AppLocalizations.dart class I'm using for the localicationDelegates

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;
  }
}

and here is where I added to the root of the project

return MaterialApp(
      localizationsDelegates: [
        AppLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: AppLocalizations.delegate.supportedLocales,
      title: 'AMP',
      theme: Theme.darkTheme,
      home: StyleguideScreen(),
    );

And here is how I try to implement it and it crashes

class StyleguideScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final welcome = AppStrings.current.welcomeGeneralInfoTitle;
    return Scaffold(...)
 }
}

The app generates correctly all the generated files per language that it needs to import and I think it looks pretty straight forward, when I debug it, it is getting the locale correctly. Has anyone any idea why could this be happening? Thanks in advance :pray:

keylin wu
  • 81
  • 1
  • 9
  • I cannot speak much to the internationalization process, as I have no experience with it. What I can tell you is that it's crashing because AppStrings.current is null. Perhaps this occurs while things are loading in the background? Try something like final welcome = AppStrings.current?.welcomeGeneralInfoTitle ?? ''; – Lee3 Nov 12 '20 at 22:09
  • Hi @Lee3 yeah, but the problem is I am not getting the result from the localization, I am not getting the string localized from the files generate. Neither with `current` nor with `context` – keylin wu Nov 13 '20 at 06:35
  • To prevent inconsistencies between different ARB-files, I recommend to use `attranslate`: https://github.com/fkirc/attranslate Semi-automating file-synchronization can save you a great deal of trouble with outdated, missing or stale translations. – Mike76 Nov 13 '20 at 14:30

2 Answers2

3

FIX: I just needed to add into the localizationsDelegates the auto-generated AppStrings.delegate, from the file import 'generated/l10n.dart'; instead of creating a new AppLocalizations.delegate. like this:

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

and remove completely the AppLocationzations class I did and it works smooth! :)

PD: I add this new library flutter_localized_locales

keylin wu
  • 81
  • 1
  • 9
0

I use Android Studio Flutter with Intl plugin and the problem was the same

In main.dart add import import 'generated/l10n.dart';

Also add S.delegate in list of localizationsDelegates

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