【问题标题】:Flutter Convert String Variable to IntegerFlutter 将字符串变量转换为整数
【发布时间】:2020-11-28 20:55:25
【问题描述】:

我想在无状态的 Widget 中将 String 变量转换为 Integer。

这是我的代码:

final String bmiresult;

Text(
                      bmiresult,
                      style: TextStyle(
                        color: bmiresult > 30 ? Colors.red : Colors.green,
                        fontSize: 40.0,
                        fontWeight: FontWeight.bold,
                      ),

上面写着The operator > isn't defined for type of String.

谢谢。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以使用 int.parse,参见 doc

    final String bmiresult;
    .
    Text(
      bmiresult,
      style: TextStyle(
      color: int.parse(bmiresult) > 30 ? Colors.red : Colors.green,
      fontSize: 40.0,
      fontWeight: FontWeight.bold,
    ),
    

    【讨论】:

    • 谢谢@h1b9b。这行得通。我很抱歉问了一个非常简单的问题。请不要投反对票。
    猜你喜欢
    • 2016-01-26
    • 2017-04-12
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    相关资源
    最近更新 更多