【问题标题】:Timestamp is having a null value when saving in Firebase在 Firebase 中保存时时间戳的值为空
【发布时间】:2021-06-20 15:18:06
【问题描述】:

我有以下代码:

      DocumentReference ref = db.collection(Paths.playersPath).doc(uid);
      var data = {
        'uid': uid,
        'name': name,
        'email': email,
        'joinDate': FieldValue.serverTimestamp(),
      };
      ref.set(data, SetOptions(merge: true));
      final DocumentSnapshot currentDoc = await ref.get();
      user = Player.fromFirestore(currentDoc);

当我将其保存在 firebase 中时,出现错误:

type 'Null' is not a subtype of type 'Timestamp'

当我通过打印显示时,它显示以下值:

FieldValue(Instance of 'MethodChannelFieldValue')

请帮助解决问题。谢谢!

【问题讨论】:

  • 你可以试试(DateTime)FieldValue.serverTimestamp(), DateTime.Parse(FieldValue.serverTimestamp().ToString())

标签: firebase flutter dart


【解决方案1】:

这个article

  1. 侦听器触发,您将获得时间戳的现有值。 2.监听器再次触发,您观察到 createdAt 字段为 null。 3.监听器再次触发,您观察到一个实际的时间戳值。

【讨论】:

    【解决方案2】:

    对于 Kotlin,Timestamp(Date()) 代替 FieldValue.serverTimestamp() 有效。要使用 Timestamp(Date()),您必须导入 com.google.firebase.Timestamp。不确定 Flutter 是否有所不同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      相关资源
      最近更新 更多