Home > Enterprise >  add time picker to flutter code causes error
add time picker to flutter code causes error

Time:10-28

I use below code to add time picker:

final TimeOfDay newTime = await showTimePicker(
  context: context,
  initialTime: TimeOfDay(hour: 7, minute: 15),
);

But I give below error:

A value of type 'TimeOfDay?' can't be assigned to a variable of type 'TimeOfDay'. Try changing the type of the variable, or casting the right-hand type to 'TimeOfDay'

CodePudding user response:

Simply change to:

final TimeOfDay? newTime = await showTimePicker(
  context: context,
  initialTime: TimeOfDay(hour: 7, minute: 15),
);

Then you may want to unwrap the optional result:

if (newTime != null) {
  // do stuff with newTime
}
  • Related