【发布时间】:2019-12-24 14:51:55
【问题描述】:
我正在尝试使用传递的类实例设置状态并访问成员值,但它给了我未在对象上定义的 getter 的错误。但 initState 方法也是如此。
这是用于在用户关闭 ShowDialog 时传递值。我尝试设置吸气剂并将班级成员从私人更改为公共。
class MyReaction {
IconData _icon;
String _text;
MyReaction(this._icon, this._text);
IconData get iconD => this._icon;
String get textV => this._text;
}
class _MyHomePageState extends State<MyHomePage> {
MyReaction _myreaction = new MyReaction(Icons.thumb_up, 'Like');
IconData _myreactionIcon;
String _myreactionText;
@override
void initState() {
super.initState();
controller = new ScrollController();
_myreactionIcon = _myreaction.iconD; //It works here!!
_myreactionText = _myreaction.textV;
}
FlatButton.icon(
icon:Icon(this._myreactionIcon, size: 24.0),
onPressed: () {
showDemoDialog<MyReaction>(
context: context,
child: SimpleDialog(
title: const Text('Your Reaction'),
children: <Widget>[
DialogDemoItem(
icon: Icons.account_circle,
color: Colors.black87,
text: 'username@gmail.com',
onPressed: () {
MyReaction _reaction = new MyReaction(Icons.account_circle, 'Like');
Navigator.pop(context, _reaction);
},
),
]
)
);
},
);
void showDemoDialog<MyReaction>({ BuildContext context, Widget child }) {
showDialog<MyReaction>(
context: context,
builder: (BuildContext context) => child,
)
.then<void>((MyReaction value) { // The value passed to Navigator.pop() or null.
if (value != null) {
setState(() {
_myreactionIcon = value.iconD; //Does not work here
_myreactionText = value.textV;});
// _scaffoldKey.currentState.showSnackBar(SnackBar(
// content: Text('You selected: $value'),
));
}
});
}
}
我希望 showDialog 方法中的值可以分配给状态变量。但是 value.iconD 给出了 getter not defined 错误。我做错了什么
【问题讨论】: