Флаттер - геттер был вызван на ноль - PullRequest
0 голосов
/ 01 ноября 2019

Я хочу добавить локализацию в свое приложение (Flutter 1.10.7, канал бета), но у меня проблемы с этим. Мне пришлось обернуть мой виджет в другой класс, чтобы вызвать его, в противном случае я получаю следующую ошибку:

The following NoSuchMethodError was thrown building MyApp(dirty):
The getter 'hello' was called on null.
Receiver: null
Tried calling: hello

Следующий код не работает:

 class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        final i18n = I18n.delegate;
        return MaterialApp(
            theme: ThemeData(
              primarySwatch: Colors.blue,
            ),
            localizationsDelegates: [i18n],
            supportedLocales: i18n.supportedLocales,
            home: Text(I18n.of(context).hello));
      }
    }

Этот работает:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final i18n = I18n.delegate;
    return MaterialApp(
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        localizationsDelegates: [i18n],
        supportedLocales: i18n.supportedLocales,
        home: MyWidget());
  }
}

class MyWidget extends StatelessWidget {   
  @override
  Widget build(BuildContext context) {
    return Text(
      I18n.of(context).hello,
    );
  }
}

Может кто-нибудь объяснить мне, почему это происходит?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...