在 Text 小部件颤动中打印 int 值 [英] Printing int value in Text widget flutter

查看:31
本文介绍了在 Text 小部件颤动中打印 int 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在 Text 小部件中打印 int 值?我尝试使用 '$num' 但它只是作为 num 的实例出现.

How do we print the int value in Text widget? I tried using '$num' but it just come out as instance of num.

这是我目前尝试过的

Text(
   '$widget.count'+'. ',
    style: TextStyle(
    fontFamily: 'sansPro',
    fontSize: 15,
    color: Colors.black,
    fontWeight: FontWeight.bold,
      ),
    ),

推荐答案

在你的具体情况下你需要用 {} 包裹数据,所以更新代码如下,

In your specific case you need to wrap the data with {}, So update the code to the following,

Text(
   '${widget.count}''. ',
    //...
);

了解更多详情:

打印"或渲染"num 值是 dart 中的 intdouble 你可以使用 .toString() 方法变量.您还可以使用字符串插值"即对 String 中的变量或表达式求值.

To "print" or "render" a num value that is an int or a double in dart you can use the .toString() method on the variable. You can also use "String Interpolation" that is to evaluate a variable or an expression in a String.

例如创建一个渲染变量 X 的 Text 小部件:

For Example to create a Text widget that renders the a variable X:

Text(x.toString());

并且使用字符串插值,您可以使用:

And using String Interpolation, you can use:

Text("$x");

如果变量需要作为表达式的一部分进行计算,也就是说,如果变量是 Object 的一部分,或者您想在计算最终值之前应用一个操作,那么表达式需要包含在 {} 中,其代码为

If the variable needs to be evaluated as a part of an expression, That is if the vailable is part of an Object or you want to apply an operation before evaluating the final value, then the expression needs to be wrapped within {}, the code for that would be

Text("${x * 100}");

这篇关于在 Text 小部件颤动中打印 int 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆