【问题标题】:Getter isn't defined for the object没有为对象定义 Getter
【发布时间】: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 错误。我做错了什么

【问题讨论】:

    标签: flutter dart


    【解决方案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;
            });
           }
        }
      );
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-22
      • 1970-01-01
      • 2020-08-08
      • 2021-06-08
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      相关资源
      最近更新 更多