直接打印实例属性以在 Dart 中进行控制台的正确语法(不仅仅是打印范围内的变量)

Proper syntax for directly printing instance properties to console in Dart (not simply printing a variable that is in scope)

在 Dart/Flutter 中,假设您有 Class Y 的实例 a

Class Y 有一个 属性, 属性1.

您想像这样使用字符串插值来打印 属性:

print('the thing I want to see in the console is: $a.property1');

但您甚至无法在不出现错误的情况下完成输入。

我让它工作的唯一方法是这样做:

var temp = a.property1;
print ('the thing I want to see in the console is: $temp');

我还没有在网上找到答案...而且我认为必须有一种方法可以直接做到这一点,而不必先创建一个变量。

您需要将 属性 括在花括号中:

print('the thing I want to see in the console is: ${a.property}');

然后将打印 a.property 的值。

看来你也可以这样做,但似乎没有任何地方记录:

print('..... $a.$property1');