如何在 Flutter 中传递 return 小部件的三元组

How to pass ternary that return a widget in Flutter

你好,我想在我的小部件中使用三元来创建一个条件...但我无法通过它...错误显示 type '()=>Null' is not a subtype of type 'Widget' 这是代码

DateFormat("HH:mm:ss").format(DateTime.now()) =="05:12:01"
?
// the error starts from here since I have 2 commands (setState and widget Text) inside function
() {
setState(() {
  fail = true;
  _imageFile = null;
});
Text('Time is up');
}
: Text("Success")

我应该怎么做才能获得 Text('Time is up');

setState(() {
  fail = true;
  _imageFile = null;
});

将它包装在函数中是个坏主意吗?

Widget build(BuildContext context) {
  return someCondition ? SomeWidget() : OtherWidget();//someCondition is a bool defined in your class
}

在您提供的代码中,您应该放置一个 if 语句,您不能将语句放在条件表达式中