pinchbv / floor

The typesafe, reactive, and lightweight SQLite abstraction for your Flutter applications
https://pinchbv.github.io/floor/
Apache License 2.0
965 stars 191 forks source link

Support for private fields with getters #568

Open gavv opened 3 years ago

gavv commented 3 years ago

Hi! Thanks a lot for this library.

I've the following model class:

@entity
class Foo {
  Foo(this._field, ...);

  @ColumnInfo(name: 'field')
  int _field = -1;
  int get field => _field;

  ...

It has a private field _field with a public getter field and no setter. Foo is immutable: fields are set in constructor and then remain unchanged.

If I understand correctly, this use case is not supported by floor currently. Floor generates code that tries to access _field and I didn't find a way to tell to use field instead.

Am I missing something? If not, it would be great if floor allowed working with immutable models somehow.

ramsestom commented 3 years ago

your field necessarily need both a setter and a getter is you want Floor to be able to use it as a database column. Here only _field can be set (via the Constructor) but not field. So Floor has no way to populate back the field attribute when a value is read from the database. So you won't be able to use field as a database column until you also give him a setter.

mqus commented 3 years ago

Floor actually only uses the constructor and not setters(iirc) so this is a reasonable request imho.

SEGVeenstra commented 1 year ago

If we would want to make these entities immutable, we could take a look at how freezed is doing this? I guess it would change things drastically as we should have a copyWith to update the model when we no longer are able to change the values directly.