Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
897 views
in Technique[技术] by (71.8m points)

How to store Timestamp in firestore using flutter-web

I can't save a Timestamp object in Firebase Firestore using Flutter Web, with Android or iOS it works normally

Already tried:

  • FieldValue.serverTimestamp()
  • DateTime.now()
  • Timestamp.now()
  • Converting from MicrosecondsSinceEpoch()

My code:

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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As Andres S mentioned in the comments, I've updated the SDK, and also imported the "cloud_firestore_web" as dependency in pubspec.yaml.

This solution worked fine with DateTime.now()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...