根据官方国际化教程的提示,当我尝试使用自动生成的ARB文件翻译引擎(AppLocalizations.of(context).helloWrold
(时,我得到了The getter was called on null
:
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
class Login extends StatelessWidget {
final loginAction;
final String loginError;
const Login(this.loginAction, this.loginError);
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: () {
loginAction();
},
child: Text(AppLocalizations.of(context).helloWorld),
),
Text(loginError ?? ''),
],
);
}
}
这个问题的解决方案是官方教程中缺少的部分。这里没有描述为允许引擎运行,需要在main.dart
文件中声明额外的委托。这可以从GitHub存储库的例子中得出结论。要解决此问题,请转到MaterialApp
并添加缺少的委托AppLocalizations.delegate
:
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
child: MaterialApp(
localizationsDelegates: [
// ... app-specific localization delegate[s] here
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', ''),
const Locale('pl', '')
],
home: Scaffold(