假设我有一个返回字符串的愚蠢函数:
String oddMicrosecond() {
if (DateTime.now().microsecondsSinceEpoch%2==0) {
return "we're even";
} else {
return "that's odd";
}
}
Text(oddMicrosecond());
是否可以将其编写为 Text
声明中的内联匿名函数而不单独定义它?
最佳答案
你不需要那个函数
Text(DateTime.now().microsecondsSinceEpoch%2==0 ? "that's odd" : "we're even")
你可以
Text(() {
if (DateTime.now().microsecondsSinceEpoch%2==0) {
return "we're even";
} else {
return "that's odd";
}
}();
)
关于dart - 如何编写返回字符串的内联函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54783204/