Closed Purus closed 2 years ago
Some more details on my use case.. Group
is a collection on its own. and it's also a sub-collection under AppUsers
/users
/groups
/users/*/groups
Thanks for the report.
/cc @rrousselGit
The ability to define the class for a Firestore sub-collection in a separate file would be nice. Let's say we have two models, User
and MedicalRecord
.
Case 1 | Collections defined in one file. Generator runs without errors, but the generated file contains the error undefined Name '_$MedicalRecordFieldMap'
.
part 'user.g.dart';
@Collection<User>('users')
@Collection<MedicalRecord>('users/*/medical_records')
@firestoreSerializable
class User {
String name;
int age;
User(
this.name,
this.age,
);
}
part 'medical_record.g.dart';
@firestoreSerializable
class MedicalRecord {
Timestamp date;
String description;
MedicalRecord(
this.date,
this.description,
);
}
Case 2 | Collections defined in two separate files. Generator doesn't run without errors
part 'user.g.dart';
@Collection<User>('users')
@firestoreSerializable
class User {
String name;
int age;
User(
this.name,
this.age,
);
}
part 'medical_record.g.dart';
@Collection<MedicalRecord>('users/*/medical_records')
@firestoreSerializable
class MedicalRecord {
Timestamp date;
String description;
MedicalRecord(
this.date,
this.description,
);
}
It would be awesome if one of the two options would work in the future, as one file with the classes for all sub-collections can get pretty large and complex. It would be neat to have one file for each class!
About this, I don't think that's something that can fully be supported.
The ODM relies on private code generated by json_serializable for a bunch of things, with more to come.
We could have a nice error message about it. But supporting it, I'm not sure.
Case 1 | Collections defined in one file. Generator runs without errors, but the generated file contains the error undefined Name '_$MedicalRecordFieldMap'.
That's your mistake. The documentation mentions that a specific json_serializable flag needs to be enabled for the ODM to handle json_serializable
See https://github.com/firebase/flutterfire/blob/master/packages/cloud_firestore_odm/doc/defining-models.md#defining-models. Cf the createFieldMap: true,
in @JsonSerializable
As mentioned, I doubt there's a reasonable solution to this problem.
Maybe this could be done better once we have metaprogramming. Otherwise, this would require a very large refactoring of json_serializable
for this.
Bug report
Describe the bug Sub-collection models from separate dart file does not seems to be working fine in the root model.
Steps to reproduce
Steps to reproduce the behavior:
The root model is below.
The other model which has its own collection and also its part of the root model is shared below.
Below is my
build.yml
content.Below is the exception after the generation.
Expected behavior
The models should be generated without any issues.