未为对象定义吸气剂

时间:2019-08-19 11:28:00

标签: flutter dart

我正在尝试使用传递的类实例来设置状态并访问成员值,但这给了我未在对象上定义的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未定义的错误。我在做什么错

1 个答案:

答案 0 :(得分:1)

您已在方法showDemoDialog中添加了一个通用类型参数,该参数遮盖了(隐藏)了对类MyReaction的定义。

此通用类型参数实际上未在您的方法中使用,因此可以将其删除。

只需这样定义您的方法:

void showDemoDialog({ BuildContext context, Widget child }) {
  showDialog<MyReaction>(context: context, builder: (BuildContext context) => child)
    .then((MyReaction value) { // The value passed to Navigator.pop() or null.
      if (value != null) {
        setState(() {
          _myreactionIcon = value.iconD; // Does now work
          _myreactionText = value.textV;
        });
       }
    }
  );

}
相关问题