简体   繁体   English

错误类型“Null”不是“Timestamp”类型的子类型

[英]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.

相关问题 Flutter 应用程序错误 - 类型“Timestamp”不是“DateTime”类型的子类型 - Flutter app error - type 'Timestamp' is not a subtype of type 'DateTime' 未处理的异常:“Null”类型不是“BuildContext”类型的子类型 - Unhandled Exception: type 'Null' is not a subtype of type 'BuildContext' dart/flutter :: type 'Null' is not a subtype of type 'String' in type cast ERRor,无法获取数据 - dart/flutter:: type 'Null' is not a subtype of type 'String' in type cast ERRor, not able to fetch data 输入“未来<null> ' 不是 flutter 和 Firebase 中类型转换中类型 'String' 的子类型</null> - type 'Future<Null>' is not a subtype of type 'String' in type cast in flutter and Firebase “Null”类型不是“List”类型的子类型<int> ' 在 flutter 的类型中</int> - type 'Null' is not a subtype of type 'List<int>' in type cast in flutter _TypeError 在构建 NotesListView(dirty) 时被抛出:“Null”类型不是“String”类型的子类型 - _TypeError was thrown building NotesListView(dirty) : type 'Null' is not a subtype of type 'String' 输入'_InternalLinkedHashMap<string, dynamic> ' is not a subtype of type 'String' 显示此错误</string,> - type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' shows this error 颤振类型'未来<dynamic> ' 不是类型 'Widget' 错误的子类型</dynamic> - Flutter type 'Future<dynamic>' is not a subtype of type 'Widget' error Flutter 返回错误类型 'List<dynamic> ' 不是类型 'String' 的子类型</dynamic> - Flutter returning error type 'List<dynamic>' is not a subtype of type 'String' 错误:键入 '() => Map<string, dynamic> ?' 不是类型“Map”的子类型<string, dynamic> ?'</string,></string,> - Error: type '() => Map<String, dynamic>?' is not a subtype of type 'Map<String, dynamic>?'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM