简体   繁体   English

在 Text 小部件颤动中打印 int 值

[英]Printing int value in Text widget flutter

How do we print the int value in Text widget?我们如何在 Text 小部件中打印 int 值? I tried using '$num' but it just come out as instance of num.我尝试使用 '$num' 但它只是作为 num 的实例出现。

This is what i tried so far这是我到目前为止尝试过的

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}''. ',
    //...
);

And for more details:有关更多详细信息:

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.要在 dart 中“打印”或“渲染”一个intdoublenum值,您可以在变量上使用.toString()方法。 You can also use "String Interpolation" that is to evaluate a variable or an expression in a String .您还可以使用“字符串插值”是评价一个变量或在一个表达式String

For Example to create a Text widget that renders the a variable X:例如,创建一个呈现变量 X 的Text小部件:

Text(x.toString());

And using String Interpolation, you can use:并使用字符串插值,您可以使用:

Text("$x");

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如果变量需要作为表达式的一部分进行计算,也就是说,如果 vailable 是Object一部分,或者您想在计算最终值之前应用一个操作,那么表达式需要包含在{} ,代码因为那将是

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

Text widget accept only data in a String format so you can get value in a int or double format and while displaying it convert it into String format.文本小部件仅接受 String 格式的数据,因此您可以获取 int 或 double 格式的值,并在显示时将其转换为 String 格式。 Consider a code snippet like a below:考虑如下代码片段:

intValueCount.toString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM