firebase - 在Flutter Cloud Firestore中, 'DateTime'类型不是 'String'类型的子类型

标签 firebase flutter dart google-cloud-firestore dart-pub

我正在尝试获取DateTime.now()并使用Cloud FireStore进行写入/读取。但是得到了错误

这是我的代码

在用户界面中

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";

关于firebase - 在Flutter Cloud Firestore中, 'DateTime'类型不是 'String'类型的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57510760/

相关文章:

flutter - 如何链接多个动画对象?

Flutter 如何在计时器结束时触发对话框

flutter - 如何在 dart/flutter 中收听流值

json - 类型 'Future<List<Question>>' 的值不能分配给类型 'List<Question>' 的变量

http - 如何在flutter中使用表单数据进行http发布?

javascript - React 显示嵌套数组项

firebase - 尝试按照部署指令部署多个功能导致错误,arg 太多

flutter - 对象列表中的 Dart flutter 最大值

javascript - Firebase 中的项目未显示在 Angular Grid 中

javascript - Firebase 函数不起作用 - 插入值时如何写入值