【问题标题】:The method 'call' was called on null. Flutter在 null 上调用了方法“call”。扑
【发布时间】:2020-11-15 02:22:30
【问题描述】:

我正在用颤振构建一个计算器,我试图将带有参数的回调 onclick 函数传递给位于不同文件中的按钮小部件,但是当我单击任何按钮时,它会引发异常,该方法在 null 上被调用.我也不知道如何在 CustomBtn 类中声明一个带参数的函数。

这是我传递函数的主要小部件:

CustomBtn(
              btext: '8',
              color: Colors.grey[600],
              textColor: Colors.grey[50],
              onClick: buttonPressed('8'),
            ),

这是按钮小部件:

  class CustomBtn extends StatelessWidget {
  final String btext;
  final color;
  final textColor;
  final Function onClick;

  CustomBtn({
    this.btext,
    this.color,
    this.textColor,
    this.onClick,
  });
  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      child: Text(
        btext,
        style: TextStyle(fontSize: 35.0, color: textColor),
      ),
      onPressed: () => onClick(btext),
      color: color,
      padding: EdgeInsets.fromLTRB(0.0, 24.0, 0.0, 24.0),
    );
  }
}

【问题讨论】:

    标签: function flutter dart callback


    【解决方案1】:

    当您将函数传递给onClick 参数时,您正在调用该函数。相反,只需将引用传递给函数。

    CustomBtn(
        btext: '8',
        color: Colors.grey[600],
        textColor: Colors.grey[50],
        onClick: buttonPressed,
    ),
    

    【讨论】:

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