我调用了要在注册屏幕上使用的showDatePicker函数。由于我使用getx编写,所以我使用obs来即时显示所选值,但是我遇到了如下错误:
类型'Rx'不是类型'DateTime'的子类型
class RegisterScreen extends GetWidget<RegisterController> {
RegisterScreen({Key? key}) : super(key: key);
static const routeName = "/register_screen";
DateTime _selectedDate = DateTime.now().obs as DateTime;
late BuildContext _context;
Obx(() => GestureDetector(
onTap: (() async {
var initialDate = DateTime.now();
_selectedDate = await showDatePicker(
context: context,
initialDate: initialDate,
firstDate: DateTime(1930),
lastDate: DateTime(2100),
) ??
_selectedDate;
print(_selectedDate.toIso8601String());
}),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
const Icon(
FontAwesomeIcons.calendar,
size: 30,
),
Expanded(
child: Text(
DateFormat("EEE,MMM d")
.format(_selectedDate),
textAlign: TextAlign.center,
),
),
],
),
),
),
)),
你需要改变这个:
DateTime _selectedDate = DateTime.now().obs as DateTime;
:
var _selectedDate = DateTime.now().obs;
并且像这样使用_selectedDate
的值:
_selectedDate.value
点击这里了解更多关于GetX的信息。