+-
FlutterDart: 在Widget Param中把int转为double不工作

我试图在flutterdart中用Text Widget的param fontSize将int转换为double,但我遇到了这个异常。

"Another exception was thrown: type 'int' is not a subtype of type 'double' in type cast"

我一直按照这里的说明进行操作。为什么我不能把一个数字转换成双数? 但这还不行。

这里是我的组件部件内的文本。

class ButtonLogin extends MaterialButton {
  ButtonLogin({this.fontSize, ...});

final fontSize;
...

@override
Widget build(BuildContext context){
double fontSizeDouble = (fontSize as num) as double; 
...
Text(
 label,
 style: TextStyle(
   fontSize: fontSizeDouble,
   color: labelColor,
   fontWeight: FontWeight.bold
 ),
),
...
}

从组件中输出,我在param fontSize中传递了一个Int值,就像这样。

...
 ButtonLogin(
      backgroundColor: ColorsCustom.loginScreenUp,
      labelColor: Colors.white,
      label: 'Back to other session?',
      fontSize: 16,
      mOnPressed: () => _login(),
 );
...

省略号只是为了表示还有更多的代码,它不是代码的一部分。

0
投票

我没有收到任何错误。为了避免错误,我可以给你一个建议,就是在你定义变量时,在代码中使用数据类型。