在 null 上调用了方法 'call'。扑
The method 'call' was called on null. Flutter
我正在用 flutter 构建一个计算器,我试图将带有参数的回调 onclick 函数传递给位于不同文件中的按钮小部件,但是当我单击任何按钮时,它会抛出调用该方法的异常在空。我也不知道如何在 CustomBtn class.
中声明带有参数的函数
这是我传递函数的主要小部件:
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),
);
}
}
当您将函数传递给 onClick
参数时,您正在调用该函数。相反,只需将引用传递给函数。
CustomBtn(
btext: '8',
color: Colors.grey[600],
textColor: Colors.grey[50],
onClick: buttonPressed,
),
我正在用 flutter 构建一个计算器,我试图将带有参数的回调 onclick 函数传递给位于不同文件中的按钮小部件,但是当我单击任何按钮时,它会抛出调用该方法的异常在空。我也不知道如何在 CustomBtn class.
中声明带有参数的函数这是我传递函数的主要小部件:
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),
);
}
}
当您将函数传递给 onClick
参数时,您正在调用该函数。相反,只需将引用传递给函数。
CustomBtn(
btext: '8',
color: Colors.grey[600],
textColor: Colors.grey[50],
onClick: buttonPressed,
),