![](/img/trans.png)
[英]flutter: type '_InternalLinkedHashMap<Object?, Object?>' is not a subtype of type 'Map<String, dynamic>'
[英]Flutter: type 'DateTime' is not a subtype of type 'String'
Text(
DateTime.parse(documents[index]['createdAt']
.toDate()
.toString()) ??
'default',
)
我正在嘗試從 firestore 獲取日期。 我將 DateTime.now() 存儲在 firestore 的 createdIn 中。
因為兩者的格式不一樣。
嘗試閱讀此內容: https ://help.talend.com/r/6K8Ti_j8LkR03kjthAW6fg/atMe2rRCZqDW_Xjxy2Wbqg
在這里,它期望類型為“String”,但我們將其作為“DateTime”傳遞,這是不正確的。
以默認的“en_US”格式格式化日期不需要任何初始化。
https://api.flutter.dev/flutter/intl/DateFormat-class.html
因此,我們只需要將其格式化為:
var dateTime = DateTime.now()
DateFormat.E().format(dateTime)
E() 是 Flutter 中 DateFormat 的構造函數,它指的是星期的前 3 個字母
您可以參考官方文檔以獲取可用的構造函數:
https://api.flutter.dev/flutter/intl/DateFormat-class.html#constructors
注意:以下是用於 DateTime 庫的版本 ~ intl intl: ^0.18.0
如果它是時間戳字段,則將其轉換為時間戳。 如果您需要它作為 DateTime,請在其上使用 a.toDate。 不要通過字符串處理它......完全沒有必要。
它可能對你有幫助
String? changeDateFormatWithInput(String? currentDate, currentFormat,
requiredFormat) {
String? date;
try {
if (currentDate != null && currentDate.isNotEmpty) {
DateTime tempDate =
new DateFormat(currentFormat).parse(currentDate, true);
date = DateFormat(requiredFormat).format(tempDate.toLocal());
return date;
}
} catch (e) {
print(e);
return null;
}
return date;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.