I want to fetch meetings from Firestore and map them into the following Meeting
model:
part 'meeting.g.dart';
@JsonSerializable(explicitToJson: true)
class Meeting {
String id;
DateTime date;
Meeting(this.id, this.date);
factory Meeting.fromJson(Map<String, dynamic> json) {
return _$MeetingFromJson(json);
}
Map<String, dynamic> toJson() => _$MeetingToJson(this);
}
The documents are fetched from Firestore and then fromJson
is called on the iterable, but an exception is thrown:
type 'Timestamp' is not a subtype of type 'String' in type cast
When I go into generated meeting.g.dart
, it's this line which causes the error
json['date'] == null ? null : DateTime.parse(json['date'] as String)
To workaround the issue, I've tried changing from DateTime to Timestamp in the model, but then the following build error is shown:
Error running JsonSerializableGenerator
Could not generate `fromJson` code for `date`.
None of the provided `TypeHelper` instances support the defined type.
Could you tell me how do you solve this issue? Is there another preferred way to combine Firebase and a Flutter project using json_serializable for JSON serialization? Maybe even replace usage of json_serializable ?