Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localizations.of return null from StatelessWidget

Tags:

flutter

As the title said, if the widget class is extended from StatelessWidget

Localizations.of<MaterialLocalizations>(context, MaterialLocalizations);

will return null any ideas?

like image 221
DeckyFx Avatar asked Jun 29 '18 05:06

DeckyFx


1 Answers

had the same problem. Theres an open github issue in the flutter repository. Only thing that solved it for me was creating a separat widget like this:

@override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        localizationsDelegates: [...],
        supportedLocales: ..,
        locale: ...,
        home: MyAppPage()); // separat MyAppPage instead of putting widget code with translations here
  }

// MyAppPage
class MyAppPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        ...
        body: Center(
            child: Text(AppTranslations.of(context).translate(...)),

If this is not solving your problem we maybe need to see more code :)!

like image 103
novas1r1 Avatar answered Oct 19 '22 12:10

novas1r1