接受 变量 名, 打印 "变量名":变量
可以使用 Dart 中的字符串插值和 print 函数来实现该方法封装:
void prettyPrint(String variableName, dynamic variable) {
print('$variableName: $variable');
}
该方法接受两个参数,variableName 表示要打印的变量名,variable 表示要打印的变量。使用字符串插值将变量名和变量值拼接起来,传入 print 函数进行输出即可。
使用示例:
int foo = 123;
prettyPrint('foo', foo); // 输出:foo: 123
String bar = 'Hello world!';
prettyPrint('bar', bar); // 输出:bar: Hello world!