Search code examples
flutterdartlocalizationflutter-widget

flutter: supported language is throwing error (Localizations.of<MaterialLocalizations>(context, MaterialLocalizations) returns null)


Every time startting my app I get the following warning in the log:

════════
I/flutter (22601): Warning: This application's locale, de, is not supported by all of its
I/flutter (22601): localization delegates.
I/flutter (22601): > A MaterialLocalizations delegate that supports the de locale was not found.
I/flutter (22601): See flutter.dev/tutorials/internationalization for more
I/flutter (22601): information about configuring an app's locale, supportedLocales,
I/flutter (22601): and localizationsDelegates parameters.
I/flutter (22601):
════════

Also, when trying to use various widgets likeAppBar or when trying to show a dialog I can't do that because flutter throws following assert (exception):

No MaterialLocalizations found.

since

Localizations.of<MaterialLocalizations>(context, MaterialLocalizations) == null

assertion is true.

German is listed as a supported locale, and I have registered supportedLanguages as well as localizationDelegates in my MaterialApp. Since I use MaterialApp as the main root StatelessWidget, I don't get why this error is thrown. I have already searched and found not much, just this old question with no real answer to it, which is the same problem. There is also a GitHub issue to this problem, but it seems like not really many others have the same problem.


Solution

  • For any future reader, I found the issue: I didn't include the package:

    flutter_localizations:
      sdk: flutter
    

    and also didn't include the GlobalMaterialLocalization delegates:

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