2

I'm working with Objectbox (1.3.0) to build my database on Flutter.

I try to create an entity composed of a custom type (enumeration) like this :

type_enum.dart

/// Type Enumeration.
enum TypeEnum { one, two, three }

method.dart

@Entity()
class Method {
  /// ObjectBox 64-bit integer ID property, mandatory.
  int id = 0;

  /// Custom Type. 
  TypeEnum type;

  /// Constructor.
  Method(this.type);

  /// Define a field with a supported type, that is backed by the state field.
  int get dbType {
    _ensureStableEnumValues();
    return type.index;
  }

  /// Setter of Custom type. Throws a RangeError if not found.
  set dbType(int value) {
    _ensureStableEnumValues();
    type = TypeEnum.values[value];
  }

  void _ensureStableEnumValues() {
    assert(TypeEnum.one.index == 0);
    assert(TypeEnum.two.index == 1);
    assert(TypeEnum.three.index == 2);
  }
}

The previous code cause this error (after run this command dart run build_runner build :


[WARNING] objectbox_generator:resolver on lib/entity/method.dart:
  skipping property 'type' in entity 'Method', as it has an unsupported type: 'TypeEnum'
[WARNING] objectbox_generator:generator on lib/$lib$:
Creating model: lib/objectbox-model.json
[SEVERE] objectbox_generator:generator on lib/$lib$:

Cannot use the default constructor of 'Method': don't know how to initialize param method - no such property.

I would like to construct method by given a type by constructor parameter. What it's wrong ?

If I remove constructor, I should add late identifier in front of type field. I don't want to do this. May be, I don't understand. I haven't found any example.


My solution :

method.dart

@Entity()
class Method {
  /// ObjectBox 64-bit integer ID property, mandatory.
  int id = 0;

  /// Custom Type. 
  late TypeEnum type;

  /// Constructor.
  Method(int dbType){
     this.dbType = dbType;
  }

  /// Define a field with a supported type, that is backed by the state field.
  int get dbType {
    _ensureStableEnumValues();
    return type.index;
  }

  /// Setter of Custom type. Throws a RangeError if not found.
  set dbType(int value) {
    _ensureStableEnumValues();
    type = TypeEnum.values[value];
  }
}
Dev Loots
  • 708
  • 9
  • 28

1 Answers1

1

For ObjectBox to be able to construct objects read from the database, entities must have a default constructor or a constructor with argument names matching the properties.

For example to get a default constructor in this case, make the parameter optional and provide a default value:

Method({this.type = TypeEnum.one});

Or add a default constructor and make the one requiring a type a named constructor:

Method() : type = TypeEnum.one;
Method.type(this.type);
Uwe - ObjectBox
  • 1,012
  • 1
  • 7
  • 11
  • Why constructor `Method(this.type);` not working ? – Dev Loots Jan 25 '22 at 09:07
  • I Get a Serve error now : [SEVERE] objectbox_generator:generator on lib/$lib$: Cannot use the default constructor of 'Method': don't know how to initialize param type - no such property. – Dev Loots Jan 25 '22 at 09:08