You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am having a lot of trouble with firestoreJsonConverters and DateTimes.
I want to serialize a model which uses a DateTime to json and then encode it. I am currently using this model with the firestore ODM and it works fine. It can be simplified to the followingFoo class:
Now, I would like to also allow this model to be used outside of firestore and be encoded to a String:
test('encode to json', () {
final foo =Foo(date:DateTime.now());
jsonEncode(foo.toJson()); // <- this line throws
});
The error:Converting object to an encodable object failed: Instance of 'Timestamp'.
What exactly is going on here? If I force Foo.date to use FirestoreTimestampConverter, this test will not throw but the ODM will fail to decode Foo, with the error: Instance of 'Timestamp': type 'Timestamp' is not a subtype of type 'String' at dart:sdk_internal 3747:11
If, however, Foo is set to use FirestoreDatetimeConverter I get the previous issue.
Any help is much appreciated, I am a bit puzzled as to how to fix this, and have not found issues about this. Can I not use the same model for both firestore ODM and json encoding?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I am having a lot of trouble with
firestoreJsonConverters
andDateTime
s.I want to serialize a model which uses a
DateTime
to json and then encode it. I am currently using this model with the firestore ODM and it works fine. It can be simplified to the followingFoo
class:Now, I would like to also allow this model to be used outside of firestore and be encoded to a
String
:The error:
Converting object to an encodable object failed: Instance of 'Timestamp'
.What exactly is going on here? If I force
Foo.date
to useFirestoreTimestampConverter
, this test will not throw but the ODM will fail to decodeFoo
, with the error:Instance of 'Timestamp': type 'Timestamp' is not a subtype of type 'String' at dart:sdk_internal 3747:11
If, however,
Foo
is set to useFirestoreDatetimeConverter
I get the previous issue.Any help is much appreciated, I am a bit puzzled as to how to fix this, and have not found issues about this. Can I not use the same model for both firestore ODM and json encoding?
Beta Was this translation helpful? Give feedback.
All reactions