Closed Jonas-Sander closed 6 years ago
There's already an alternative serializer--just need to install it:
Apologies for brevity, posted from phone.
@dave26199 Thank you very much for answering, unfortunately I still get an error message. It is now:
E/flutter ( 8649): Deserializing '[subject, Geschichte, todoUntil, 2018-07-16 04:00:00.000, forUsers, {userID1:...' to 'Homework' failed due to: Deserializing '2018-07-16 04:00:00.000' to 'DateTime' failed due to: type 'DateTime' is not a subtype of type 'String' in type cast
E/flutter ( 8649): #0 BuiltJsonSerializers._deserialize (package:built_value/src/built_json_serializers.dart:151:11)
E/flutter ( 8649): #1 BuiltJsonSerializers.deserialize (package:built_value/src/built_json_serializers.dart:102:18)
E/flutter ( 8649): #2 BuiltJsonSerializers.deserializeWith (package:built_value/src/built_json_serializers.dart:32:12)
E/flutter ( 8649): #3 parseHomework (package:sharezone/models/homework.dart:34:60)
E/flutter ( 8649): #4 main (file:///C:/Users/jonas/AndroidStudioProjects/sharezone/lib/main.dart:31:32)
E/flutter ( 8649): <asynchronous suspension>
E/flutter ( 8649): #5 _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:279:19)
E/flutter ( 8649): #6 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:165:12)
So now instead of saying
type 'DateTime' is not a subtype of type 'int' in type cast
it say's
type 'DateTime' is not a subtype of type 'String' in type cast
Ich changed the line:
final standardSerializers =
(serializers.toBuilder()..addPlugin(new StandardJsonPlugin())).build();
to
final standardSerializers =
(serializers.toBuilder()..addPlugin(new StandardJsonPlugin())..add(Iso8601DateTimeSerializer())).build();
Did I do something wrong?
Ah, I understand now. The problem is that it actually does not need serializing at all, it's already a DateTime. You can do that following the instructions for GeoPoint here:
https://github.com/google/built_value.dart/issues/417
I.e. add your own "serializer" that does nothing.
Apologies for brevity, sent from phone...
@dave26199 Thank you very much for your help! Everything working now.
For anybody who has the same issue, here's the solution:
1. Make an own serializer, which does nothing
In my case:
import 'package:built_collection/built_collection.dart';
import 'package:built_value/serializer.dart';
///A Serializer which does basically "nothing".
/// It is used, as a DateTime returned by Firestore is ALREADY a DateTime Object
/// and not just "data" which has to be deserialized into a DateTime.
class DateTimeSerializer implements PrimitiveSerializer<DateTime> {
final bool structured = false;
@override
final Iterable<Type> types = new BuiltList<Type>([DateTime]);
@override
final String wireName = 'GeoPoint';
@override
Object serialize(Serializers serializers, DateTime geoPoint,
{FullType specifiedType: FullType.unspecified}) {
return geoPoint;
}
@override
DateTime deserialize(Serializers serializers, Object serialized,
{FullType specifiedType: FullType.unspecified}) {
return serialized as DateTime;
}
}
2. Add the DateTimeSerializer to your serializers
final serializers =
(_$serializers.toBuilder()..add(DateTimeSerializer())..addPlugin(new StandardJsonPlugin())).build();
I am trying to deserialize a document from Firestore into my homework object, but I get this error message:
The thing is that it works locally with DateTime.now but doesn't with the DateTime in the Firestore Document. Here is the Firestore Document returned:
homework.dart
serializers.dart
I hope it's not too much or litte... This is my first issue ;), so if I can improve something writing these let me know.