keylin wu
keylin wu

Reputation: 81

Flutter Localization with ARB - The getter was called on null

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:

Upvotes: 3

Views: 3242

Answers (2)

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,
  ],

Upvotes: 0

keylin wu
keylin wu

Reputation: 81

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

Upvotes: 3

Related Questions