[英]Error type 'Null' is not a subtype of type 'Timestamp'
I am setting the current date in firebase and trying to retrieve it to use as a reference.我将当前日期设置为 firebase 并尝试检索它以用作参考。 But somehow the results vary.但不知何故,结果有所不同。 Sometimes I'm having a null value but sometimes it's okay.有时我有一个 null 值,但有时没关系。 I tried to add a delay command but it is still behaving the same.我试图添加一个延迟命令,但它仍然表现相同。 Please help.请帮忙。 Thanks!谢谢!
//set current date
await db.collection(Paths.playersPath).doc(uid).set({
'currDate': FieldValue.serverTimestamp(),
}, SetOptions(merge: true));
DocumentSnapshot<Map<String, dynamic>> documentSnapshot =
await db.collection(Paths.playersPath).doc(uid).get();
Timestamp currDate = documentSnapshot.data()!['currDate'];
Edit: I'm not really sure if it is connected but I have 2 methods that call the same logic.编辑:我不太确定它是否已连接,但我有 2 个调用相同逻辑的方法。 One is under a HookWidget which almost always fail and another in StatefulWidget which never have a problem.一个在几乎总是失败的 HookWidget 下,另一个在 StatefulWidget 下,从来没有问题。
Edit: I don't really know why.编辑:我真的不知道为什么。 But when I move the event which update the current date to a Stateful Widget, it is now working.但是当我将更新当前日期的事件移动到有状态小部件时,它现在可以工作了。 Hopefully someone can clarify this.希望有人能澄清这一点。
Can you try this:你能试试这个吗:
db.collection(Paths.playersPath).doc(uid).get().then((value) => value.data();['currDate']);
You can also try to chain then with db.collection(Paths.playersPath).doc(uid).set
if still not working如果仍然无法正常工作,您还可以尝试使用db.collection(Paths.playersPath).doc(uid).set
进行链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.