如何使用flutter-web将时间戳存储在firestore中

Posted

技术标签:

【中文标题】如何使用flutter-web将时间戳存储在firestore中【英文标题】:How to store Timestamp in firestore using flutter-web 【发布时间】:2021-04-17 12:28:08 【问题描述】:

我无法使用 Flutter Web 在 Firebase Firestore 中保存 Timestamp 对象,但在 androidios 上它可以正常工作

已经试过了:

FieldValue.serverTimestamp() 日期时间.now() Timestamp.now() 从 MicrosecondsSinceEpoch() 转换

我的代码:

baseOEC.set('comentarios': FieldValue.arrayUnion([
                              
                                'comentario': _comentarios.text,
                                // 'data': DateTime.now(), //this works for mobile
                                'data': FieldValue.serverTimestamp(),
                                'user': widget.user
                              
                            ]), SetOptions(merge: true));

错误:

Error: [cloud_firestore/unknown] Invalid argument (dartObject): Could not convert: Instance of '_FieldValueServerTimestamp'
at Object.throw_ [as throw] (http://localhost:62147/dart_sdk.js:4354:11)
at document_reference_web.DocumentReferenceWeb.new.set (http://localhost:62147/packages/cloud_firestore_web/src/write_batch_web.dart.lib.js:615:21)
at set.next (<anonymous>)
at runBody (http://localhost:62147/dart_sdk.js:38020:34)
at Object._async [as async] (http://localhost:62147/dart_sdk.js:38051:7)
at document_reference_web.DocumentReferenceWeb.new.set (http://localhost:62147/packages/cloud_firestore_web/src/write_batch_web.dart.lib.js:610:20)
at cloud_firestore.DocumentReference.__.set (http://localhost:62147/packages/cloud_firestore/cloud_firestore.dart.lib.js:617:31)
at detalhes_oec_stream._DetalhesOecStreamState.new.<anonymous> (http://localhost:62147/packages/enterprise/screens/detalhes_oec_stream.dart.lib.js:3684:45)
at Generator.next (<anonymous>)
at runBody (http://localhost:62147/dart_sdk.js:38020:34)
at Object._async [as async] (http://localhost:62147/dart_sdk.js:38051:7)
at http://localhost:62147/packages/enterprise/screens/detalhes_oec_stream.dart.lib.js:3682:847
at ink_well._InkResponseState.new.[_handleTap] (http://localhost:62147/packages/flutter/src/material/icon_button.dart.lib.js:51103:42)
at tap.TapGestureRecognizer.new.invokeCallback (http://localhost:62147/packages/flutter/src/gestures/recognizer.dart.lib.js:189:18)
at tap.TapGestureRecognizer.new.handleTapUp (http://localhost:62147/packages/flutter/src/gestures/tap.dart.lib.js:395:40)
at tap.TapGestureRecognizer.new.[_checkUp] (http://localhost:62147/packages/flutter/src/gestures/tap.dart.lib.js:201:12)
at tap.TapGestureRecognizer.new.acceptGesture (http://localhost:62147/packages/flutter/src/gestures/tap.dart.lib.js:178:23)
at arena.GestureArenaManager.new.sweep (http://localhost:62147/packages/flutter/src/gestures/arena.dart.lib.js:208:31)
at binding$5.WidgetsFlutterBinding.new.handleEvent (http://localhost:62147/packages/flutter/src/gestures/binding.dart.lib.js:318:27)
at binding$5.WidgetsFlutterBinding.new.dispatchEvent (http://localhost:62147/packages/flutter/src/gestures/binding.dart.lib.js:297:24)
at binding$5.WidgetsFlutterBinding.new.dispatchEvent (http://localhost:62147/packages/flutter/src/rendering/layer.dart.lib.js:6087:13)
at binding$5.WidgetsFlutterBinding.new.[_handlePointerEventImmediately] (http://localhost:62147/packages/flutter/src/gestures/binding.dart.lib.js:268:14)
at binding$5.WidgetsFlutterBinding.new.handlePointerEvent (http://localhost:62147/packages/flutter/src/gestures/binding.dart.lib.js:241:43)
at binding$5.WidgetsFlutterBinding.new.[_flushPointerEventQueue] (http://localhost:62147/packages/flutter/src/gestures/binding.dart.lib.js:230:14)
at binding$5.WidgetsFlutterBinding.new.[_handlePointerDataPacket] (http://localhost:62147/packages/flutter/src/gestures/binding.dart.lib.js:220:65)
at Object.invoke1 (http://localhost:62147/dart_sdk.js:181069:7)
at _engine.EnginePlatformDispatcher.__.invokeOnPointerDataPacket (http://localhost:62147/dart_sdk.js:163043:15)
at _engine.PointerBinding.__.[_onPointerData] (http://localhost:62147/dart_sdk.js:163678:49)
at http://localhost:62147/dart_sdk.js:164111:26
at http://localhost:62147/dart_sdk.js:164070:16
at http://localhost:62147/dart_sdk.js:163778:11

【问题讨论】:

您是否尝试过更新 SDK 并确保按照post 中的说明导入库? 是的!它真的解决了我的问题......奇怪的是官方文档中没有提到“cloud_firestore_web:”依赖项了,但我只是包含了这个并更新了。非常感谢! 【参考方案1】:

正如 cmets 中提到的Andres S,我已经更新了 SDK,并且还在 pubspec.yaml 中导入了“cloud_firestore_web”作为依赖项。

此解决方案适用于 DateTime.now()

【讨论】:

以上是关于如何使用flutter-web将时间戳存储在firestore中的主要内容,如果未能解决你的问题,请参考以下文章

如何将 http url 链接到 flutter-web

如何从 Flutter-web 中的小部件创建图像?

如何防止platformViewRegistry出错[flutter-web]

如何使用 MySQL 存储 UNIX 时间戳

如何使用 JPA 和 Hibernate 在 UTC 时区中存储日期/时间和时间戳

如何将firebase时间戳与graphql nest js一起使用?