【发布时间】:2018-08-19 06:36:11
【问题描述】:
刚刚开始使用 Flutter/dart,正在过渡到 PHP,并且正在努力弄清楚如何将类传递给小部件。
我正在使用 Flutter 创建我的第一个 android 和 iOS 应用程序。
我正在使用国际化,并且使用我拥有的国际化类在我的初始构建页面上一切正常。但是,当将它传递给另一个小部件时,我得到:
NoSuchMethodError:getter textTitle 在 null 上被调用。
收件人:空
尝试调用:textTitle
最好的处理方法是什么?
颤振医生
[✓] Flutter (Channel beta, v0.1.5, on Mac OS X 10.13.3 17D47, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK 27.0.1)
[✓] Android Studio (version 3.0)
[✓] Connected devices (1 available)
本地化飞镖
class HnLocalizations{
HnLocalizations(this.locale);
final Locale locale;
static HnLocalizations of(BuildContext context){
return Localizations.of<HnLocalizations>(context, HnLocalizations);
}
static Map<String, Map<String, String>> _localizedValues = {
'en': {
'btnLabelLoginS1': 'Login',
'btnLabelRegisterS1': 'Sign Up'
},
;
String get s1ButtonLabelLogin =>
_localizedValues[locale.languageCode]['btnLabelLoginS1'];
class HnLocalizationsDelegate extends LocalizationsDelegate<HnLocalizations> {
const HnLocalizationsDelegate();
@override
bool isSupported(Locale locale) => ['en', 'es'].contains(locale.languageCode);
@override
Future<HnLocalizations> load(Locale locale) =>
new SynchronousFuture<HnLocalizations>(new HnLocalizations(locale)); //HnLocalizations.load(locale);
@override
bool shouldReload(HnLocalizationsDelegate old) => false;
}
主要飞镖
void main() {
runApp(new MaterialApp(
localizationsDelegates: [
const HnLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('en', 'US'), /// Americans
const Locale('en', 'GB') /// Brits
],
title: 'HN',
home: new EntryPage(),
));
}
class EntryPage extends StatelessWidget {
final HnColors _hnColors = new HnColors();
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
// !!!**** THIS WORKS AS EXPECTED ****!!!!
title: new Text(HnLocalizations.of(context).s1ButtonLabelLogin),
backgroundColor: _hnColors.transparent(),
elevation: 0.0,
),
backgroundColor: _hnColors.accent(),
body: new Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage("assets/Background_World.png"),
fit: BoxFit.fitWidth,
),
),
child: new PanelArea(),
)
);
}
}
class PanelArea extends StatelessWidget {
@override
Widget build(BuildContext context) {
HnColors _hnColors = new HnColors();
return new Container(
child: new Center(
child: new Container(
decoration: new BoxDecoration(
borderRadius: new BorderRadius.circular(15.0),
color: _hnColors.transparent()
),
child: new Column(
children: [
new Image.asset('assets/Icon_Intro_login'),
new Text(
// !!!**** ERROR IS HERE ****!!!!
HnLocalizations.of(context).s1M1HeaderTitle,
style: new TextStyle(
color: _haillioColors.white()
),
),
最好的处理方法是什么?
更新日期:2018 年 3 月 11 日
我发现如果我将所有代码移动到 main.dart 文件中。所有本地化工作正常。但是,当我将小部件移动到单独的 dart 文件中时,错误又回来了,即使所有代码都是相同的。
更新日期:2018 年 3 月 12 日 nicolás-carrascoa reference in this post 指出了正确的方向(谢谢)。问题与导入有关,已解决 in this post here 最终成为对我有用的解决方案。该示例在下面的答案中添加。
【问题讨论】:
-
为什么你不使用 package:intl?
标签: internationalization dart flutter