当前位置: 首页 > 知识库问答 >
问题:

Flutter 无法将 Firestore 时间戳转换为 DateTime

太叔俊侠
2023-03-14

尝试在Flatter中将Firestore时间戳转换为DateTime时,我总是遇到一个错误,尝试了所有的解决方案,但都没有成功(上次尝试如何将Firestore的时间戳打印为格式化的日期和时间)

下面是我的代码;

密码

factory UserModel.fromMap(Map<String, dynamic> map) {
    return UserModel(
      name: map['name'],
      email: map['email'],
      username: map['username'],
      registerDate:
          DateTime.fromMillisecondsSinceEpoch(map['registerDate'] * 1000),
      id: map['id'],
      imageUrl: map['imageUrl'],
      cvUrl: map['cvUrl'],
      phoneNumber: map['phoneNumber'],
      education: Education.fromMap(map['education']),
      location: Location.fromMap(map['location']),
      lookingForIntern: map['lookingForIntern'],
    );
  }

错误

'Timestamp' has no instance method '*'.
Receiver: Instance of 'Timestamp'

将日期时间转换为时间戳,并已更改

registerDate:
              DateTime.fromMillisecondsSinceEpoch(map['registerDate'] * 1000),

registerDate:map['registerDate']

现在得到下面的错误;

Converting object to an encodable object failed: Instance of 'Timestamp'

但是< code > print(map[' register date '])打印< code >时间戳(秒=1632412800,纳秒=0)

共有1个答案

薛兴言
2023-03-14

试试这个包来处理问题-

https://pub.dev/packages/date_format

import 'package:date_format/date_format.dart';


     DateTime datetime = stamp.toDate();
        var time = formatDate(datetime, [hh, ':', nn, ' ', am]);
 类似资料:
  • 问题内容: 我有一个日期保存在Firestore字段中作为时间戳,我想将其转换为Swift中的日期: 我尝试了以下操作,但出现错误: 错误: 我想转换为Date的原因是为了可以使用此Date扩展名模仿您在Instagram帖子上看到的时间戳: 问题答案: 可以这样做: 或者你可以这样做: 请参阅时间戳参考文档。

  • 我在火炉里有时间戳项目。我从省道代码时间戳类型中获取项。它就像是'timestamp(seconds=1590903768,nanoseconds=26999000)'一样。

  • 为了使它更容易理解:从Twitter API你将得到这种格式的字符串Date(2020年3月26日星期四11:51:30 0000)我想做的就是用DateTime格式解析这个字符串,所以我需要将格式从这种(2020年3月26日星期四11:51:30 0000)改为类似这样的(2020年4月26日11:51:30),这可能吗?谢谢:)

  • 我试图找出如何在Kotlin中将转换为,这在Java中非常简单,但我在Kotlin中找不到任何等效的。 例如:历元时间戳(1970-01-01以来的秒数)== 在Kotlin中是否有解决方案,或者我是否必须在Kotln中使用Java语法?请给我一个简单的例子来说明如何解决这个问题。提前谢谢。 这个链接不是我问题的答案

  • 但问题是我无法将数据从Firebase转换为Local。save方法将转换为Timestamp fine。 任何解决办法。

  • 如果本地时间位于UTC时区,那么如何将转换为Unix时间戳? >。