What is the best way to return a document from firebase when the id
is not coming from the json firestore returns, using a freezed data class with fromJson
/toJson
?
I want the id
to be required (to make sure I always have one - and plan to use an "uninstantiated ID" for objects that haven't been saved to firestore yet).
But if I mark id
as required, but ignore it for json generation, the fromJson
doesn't work when I try to do code generation. Is there a way to have code generation work where I specify a second parameter in the fromJson
method to pass in id separately?
Here is my base data class:
@freezed
class Habit with _$Habit {
const Habit._();
const factory Habit({
@JsonKey(includeFromJson: false, includeToJson: false) required String id,
required String title,
String? description,
}) = _Habit;
factory Habit.newEmpty({required String userId}) => Habit(
id: uninstantiatedId,
title: '',
);
// Ideally would like to have this take a string
// id in addition to the json, and set the id to
// what is passed in separately.
factory Habit.fromJson(String id, Map<String, dynamic> json) => _$HabitFromJson(json);
}