schultek / dart_mappable

Improved json serialization and data classes with full support for generics, inheritance, customization and more.
https://pub.dev/packages/dart_mappable
MIT License
135 stars 20 forks source link

Ignore fields with Realm #191

Closed matheuscsant closed 1 month ago

matheuscsant commented 2 months ago

I'm trying to ignore some fields, as there is no need for them to go to Json or get from Json, however, Realm uses constructors for its classes and I can't run the build runner command to generate the Mappable classes because of this. Is there any other way to be able to ignore the fields? or just this one? In this example I'm trying to ignore the "id", as I only use it for internal control:

@RealmModel()
@MappableClass()
class $CondicaoDePagamento with $CondicaoDePagamentoMappable {
  @PrimaryKey()
  late ObjectId id;
  @MappableField(key: "id")
  late int codigo;
  late String? descricao;
  late $EmpresaFilial? empresaFilial;

  CondicaoDePagamento toRealmObject() {
    return CondicaoDePagamento(
      ObjectId(),
      codigo,
      descricao: descricao,
      empresaFilial: empresaFilial?.toRealmObject(),
    );
  }
}
schultek commented 1 month ago

I'm not familiar with how realm works, but does it help to define a separate constructor you can annotate with @MappableConstructor()?

So you can have one default unnamed constructor for realm, and e.g. one @MappableConstructor() MyClass.mappable() that is then used by dart_mappable.

matheuscsant commented 1 month ago

Sorry, I expressed myself wrongly, in fact the dart_mappable lib uses the constructor, my problem is not in the generation of the dart_mappable, it is in the generation of the realm, when trying to generate using the command dart run realm generate (dart run build_runner build) , because the realm uses the constructor of the class in which it contains annotation, therefore, because the dart_mappable lib uses the constructor, I think it is not possible to use the two libraries together in a single file, I believe that in my case I would have to separate them into two files. In fact, when using the constructor we can ignore some fields, but as Realm uses the constructor, I cannot ignore the fields.

No constructors allowed on realm model classes

in: package:praticsipapp/repository/pratic_schema.dart:778:3
    ╷
767 │ @RealmModel()
768 │ @MappableClass()
769 │ class $CondicaoDePagamento with $CondicaoDePagamentoMappable {
    │       ━━━━━━━━━━━━━━━━━━━━ in realm model for 'CondicaoDePagamento'
... │
778 │   $CondicaoDePagamento({
    │   ^^^^^^^^^^^^^^^^^^^^ has constructor
    ╵
Remove constructor