刚刚开始使用Flutter / dart,转换为PHP,并努力弄清楚如何将类传递给小部件 .
我正在使用flutter创建我的第一个android和iOS应用程序 .
我正在使用国际化,一切正常,在我的初始构建页面上使用我的国际化课程 . 但是,当它传递给另一个小部件时,我得到:
NoSuchMethodError:在null上调用getter textTitle . Receiver:null尝试调用:textTitle
处理这个问题的最佳方法是什么?
Flutter Doctor
[✓] 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)
Localization dart
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;
}
Main Dart
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()
),
),
处理这个问题的最佳方法是什么?
Update: Mar 11 2018
我发现如果我将所有代码移动到main.dart文件中 . 所有本地化都很好 . 但是,当我将我的小部件移动到单独的dart文件中时,即使所有代码都相同,错误也会返回 .
Update: Mar 12 2018 nicolás-carrasco由a reference in this post指出正确的方向(谢谢) . 这个问题与进口问题有关in this post here这最终成为了对我有用的解决方案 . 下面在答案中添加了该示例 .
1 回答
Nicolás Carrasco指出了一个解决方案,这个问题与导致问题的原因有关by way of link to this post .
在
localization.dart
文件导入中我有:在
main.dart
文件导入中我有:这些与描述here不同
确保使用相对路径vs packages导入所有文件解决了问题 . 区别在于我的文件而不是依赖项使用相对路径 . 那部分最初难倒 .
现在我的
localization.dart
文件有以下内容 .一切都在现在 .