onTap:初始化程序错误中只能访问静态成员

时间:2019-05-10 10:30:21

标签: dart flutter

帮助,当我在myMethod小部件或onTap小部件的GestureDetector属性中调用InkWell时,我遇到了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,
   )
 );

2 个答案:

答案 0 :(得分:1)

用这样的函数替换InkWell变量

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

答案 1 :(得分:0)

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