说我有一个愚蠢的函数,它返回一个字符串:

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";
    }
  }();
)

10-08 13:19