dart - 如何编写返回字符串的内联函数?

标签 dart flutter

假设我有一个返回字符串的愚蠢函数:

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/

相关文章:

google-maps - Flutter:如何在谷歌地图中获取 map 中心的坐标?

在 flutter 中下载文件

class - 将变量从一个类移到另一个类-Dart Flutter

dart - 为 Dart 中的每个发射生成一个带有延迟的流

Flutter - 字符编码的行为不符合预期

flutter - 如何在flutter中自动唤醒应用程序?

dart - 实时 flutter

flutter - 我怎样才能 "retry"构建?

flutter - 如何在 streambuilder 中设置状态

android - Branch IO 华为集成