Issue
ShowTimePicker is easy and understandable, but I don't know where this error came from.
The code:
TimeOfDay selectedTime = TimeOfDay.now();
IconButton(icon: Icon(Icons.access_time, color: Colors.blue),
onPressed: () async {
TimeOfDay? _time = await showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
);
if (_time != null) {
selectedTime.hour = _time.hour;
}
},
),
The error shows under hour word in selectedTime.hour
:
'hour' can't be used as a setter because it's final. Try finding a different setter, or making 'hour' non-final.
Solution
Yes, if you go to definition of class TimeOfDay you'll see hour is final so you can not reset this property. But you just reset selectedTime (both hour and minute will reset). Then in initialTime property you should check selectedTime and set like below code if it's not null. Each Time pick time you'll keep last value.
onPressed: () async {
TimeOfDay? _time = await showTimePicker(
context: context,
initialTime:
selectedTime != null ? selectedTime : TimeOfDay.now(),
);
if (_time != null) {
selectedTime = _time;
// selectedTime.hour = _time.hour;
print(selectedTime.hour);
}
},
Answered By - Quyen Anh Nguyen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.