objectbox / objectbox-dart

Flutter database for super-fast Dart object persistence
https://docs.objectbox.io/getting-started
Apache License 2.0
981 stars 119 forks source link

Type "var" or "dynamic" not supported in entity variable declaration #293

Closed raphire08 closed 2 years ago

raphire08 commented 2 years ago

Create an Entity with dynamic or var as one of the fields. Run build runner. Build runner throws error. What could be the workaround to have a dynamic variable in entity class.

@Entity()
class Shape {
  Shape(this.var1, this.var2);

  int id = 0;

  var var1;

  dynamic var2;
}

Gettting this warning [WARNING] objectbox_generator:resolver on lib/entity/shape_temp.dart: skipping property 'var1' in entity 'Shape', as it has an unsupported type: 'dynamic' [WARNING] objectbox_generator:resolver on lib/entity/shape_temp.dart: skipping property 'var2' in entity 'Shape', as it has an unsupported type: 'dynamic'

vaind commented 2 years ago

To be able to store the data in binary format (as used by the database), it has to be typed statically, otherwise, the generator doesn't know how to create the right code to serialize the variable.

On the other hand, you can store just about anything, just convert it to one of the known formats (e.g. serialize yourself using JSON to a string/blob). You can read up on how to do so in the docs: https://docs.objectbox.io/advanced/custom-types

vaind commented 2 years ago

Supporting dynamic types isn't in scope, at least not at the moment. As said above, converters are an alternative. If there are more questions, feel free to reopen.