_CastError (type ‘String’ is not a subtype of type ‘DateTime’ in type cast)

huangapple go评论66阅读模式
英文:

_CastError (type 'String' is not a subtype of type 'DateTime' in type cast)

问题

只返回翻译好的部分:

我正在尝试将日期(yyyy/mm/dd)添加到Firebase,但在将_selectedDate转换为yMd格式时,在我的代码的其他位置出现了此错误:“_CastError(类型 'String'不是类型 'DateTime'中的子类型)”

声明变量:

String _selectedDate = DateFormat("yyyy/MM/dd").format(DateTime.now()).toString();

可以输入日期的地方:

MyInputField(
  title: "日期",
  hint: DateFormat.yMd().format(_selectedDate))
英文:

I am trying to add a Date (yyyy/mm/dd) to Firebase and when converting the _selectedDate to a yMd format I am getting this error somnewhere else in my code _CastError (type 'String' is not a subtype of type 'DateTime' in type cast)

Declare variable:

String _selectedDate = DateFormat("yyyy/mm/dd").format(DateTime.now()).toString();

The place where you can enter the date:

MyInputField(
title: "Date",
hint: DateFormat.yMd().format(_selectedDate))

答案1

得分: 0

我猜测提示需要一个String,而你已经将DateTime.now()转换为String。所以,你可能不被允许为_selectedDate调用format

MyInputField(
  title: "日期",
  hint: _selectedDate.toString())

或者在赋值中:

DateTime _selectedTime = DateTime.now();

你应该选择一种方式,不要同时使用两种。

英文:

I guess hint requires String and already you converted DateTime.now() to String. So, you may not be allowed to call format for _selectedDate.

MyInputField(
title: "Date",
hint: _selectedDate)

or in assignment

DateTime _selectedTime = DateTime.now();

You are supposed to user either a way, not both.

huangapple
  • 本文由 发表于 2023年2月20日 00:51:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定