Search code examples
datefluttertimeconditional-statementsternary

How to pass ternary that return a widget in Flutter


Hi I would like to make a condition using ternary inside my widget... but I didn't able to pass it... the error says that type '()=>Null' is not a subtype of type 'Widget' and here is the code

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")

what should I do to get Text('Time is up'); and

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

is that bad idea to wrap it inside function?


Solution

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

    in the code you provided you should put an if statement you can't put statements in a conditional expression