【发布时间】:2019-10-17 02:39:45
【问题描述】:
我正在尝试将时间选择器添加到我的颤振应用程序中。我正在尝试将时间格式化为 12 小时制。但问题是当我尝试传递我的时间变量来格式化时,它给了我这样的错误。
错误:无法将参数类型“Future”分配给参数类型“TimeOfDay”。
到目前为止,我就是这样做的。
DateTime _currentDate = new DateTime.now();
TimeOfDay _currentTime = new TimeOfDay.now();
Future <Null> selectedTime;
String timeText = 'Set A Time';
@override
Widget build(BuildContext context) {
/// Time Picker
MaterialLocalizations localizations = MaterialLocalizations.of(context);
String formattedTime = localizations.formatTimeOfDay(selectedTime, <--Here i am getting error
alwaysUse24HourFormat: false);
if (formattedTime != null) {
setState(() {
timeText = formattedTime;
});
}
时间选择器小部件
时间选择器(
icon: Icons.access_time,
selectedTime: '$timeText',
onPress: () {
selectedTime = showTimePicker(
context: context,
initialTime: _currentTime,
);
},
),
有什么办法可以解决这个问题。
【问题讨论】: