schultek/dart_mappable

Ignore fields with Realm

Closed this issue ยท 2 comments

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(),
    );
  }
}

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.

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