【问题标题】:onTap: only static members can be accessed in initializers erroronTap:在初始化程序错误中只能访问静态成员
【发布时间】:2019-05-10 10:30:21
【问题描述】:

帮助,当我在 GestureDetector 小部件或 InkWell 小部件的 onTap 属性中调用 myMethod 时,我收到 only static members can be accessed in initializers 错误

static String seat1Source = 'images/carSeatBlack.png';

  _stater(){
    setState(() {
        if(seat1Source == 'images/carSeatBlack.png'){
          seat1Source = 'images/carSeatGreen.png';
        }else{
          seat1Source = 'images/carSeatBlack.png';
        }
    });
    }

  var seat1 = new InkWell(
    onTap: () => _stater(),
      child: new Image.asset(
    seat1Source,
    height: 30,
    width: 60,
   )
 );

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    用这样的函数替换你的 InkWell 变量

    _buildButton(){return new InkWell(
        onTap: () => _stater(),
          child: new Image.asset(
        seat1Source,
        height: 30,
        width: 60,
       )
     );}
    

    【讨论】:

      【解决方案2】:

      除了已经提到的另一种方法是您将变量“seat1” 进入“Widget build(BuildContext context){}”函数,因为我认为你正试图将它初始化到构造函数中。

      【讨论】:

        猜你喜欢
        • 2020-08-31
        • 2020-02-09
        • 1970-01-01
        • 2020-01-05
        • 2020-01-03
        • 2020-10-26
        • 2019-01-23
        • 2018-11-13
        相关资源
        最近更新 更多