类型 'DateTime' 不是 Flutter Cloud Firestore 中类型 'String' 的子类型

type 'DateTime' is not a subtype of type 'String' in Flutter Cloud Firestore

我正在尝试使用 Cloud FireStore 获取 DateTime.now() 和 write/read。但是报错

这是我的代码

在UI

Text( 
DateFormat('dd/MM/yyyy kk:mm')
.format(DateTime
.fromMillisecondsSinceEpoch(int.parse(document[index].data['timeCreated']))),
style: timeStyle,
),

正在将数据写入 Firestore

Firestore.instance.runTransaction((transaction) async {
      await transaction.set(
        docRef,
        {'timeCreated': DateTime.now().millisecondsSinceEpoch.toString()},
      );
    });

得到这个错误

I/flutter ( 3378): Another exception was thrown: type 'DateTime' is not a subtype of type 'String'

转换为字符串:

int time = DateTime.now().millisecondsSinceEpoch;
String t = "$time";

您可以使用自定义转换器函数!,示例在这里

static String _dateTimeToEpochUs(DateTime dateTime) =>
   dateTime?.microsecondsSinceEpoch;

使用

var dateInString =  _dateTimeToEpochUs(DateTime.now())