我想将本地化添加到我的应用程序中(Flutter 1.10.7,频道beta),但是我遇到了麻烦。我必须将小部件包装在另一个类中才能调用它,否则会收到以下错误:
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,
);
}
}
有人可以解释一下为什么会这样吗?