Search code examples
flutterflutter-intl

how to use localized strings in flutter's main app


I am new to Flutter and I am building a localized app using the flutter-intl plugin. I've declared a "Title" string in the .arb file for my locales. My program is trying to use this string as follows:

  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: [
        S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: S.delegate.supportedLocales,
      title: S.current.Title,

I've tried using S.current.Title and S.of(context).Title, and in both cases when the program starts I get the following error message: enter image description here

When I change the title property to title: 'my title', the app performs hot-reload and the error screen disappears. When I change the title back to title: S.current.Title the localized title is displayed with no error.

This behavior has been consistently confirmed with Android-studio and VS-Code with web and Android targets.

I guess it has to do with Flutter's initialization sequence. What is the proper way to use localized strings as properties in the MaterialApp?


Solution

  • As documentation suggests:

    To provide a localized title instead, use onGenerateTitle