-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Freezed (immutable) support with 0 id fails due to assignment to final id #307
Comments
Not sure how we could do this. The contract of |
Ok, thanks. If I understand correctly,then, a project that uses objectbox and immutable models is required to fully manage IDs independently, right? |
Do you think it would be possible to either use models that are mutable or immutable and they declare a function that transforms given instance with zero id to an instance with id generated by ObjectBox (which is then added to database)? @Entity()
@freezed
class Book with _$Book {
factory Book({
@Id(assignBy: getBookWithId) @Default(0) int id,
}) = _Book;
}
Book getBookWithId(Book book, int id) => book.copyWith(id: id); |
Using Equatable requires entity class to be immutable. |
@tomwyr - unfortunately that still won't satisfy the current "contract" of Anyone interested in this, please upvote the original issue (at the top) so the interest can be tracked when prioritizing development. |
This works btw! ( note: this will make your properties mutable else everything will be same ) @Freezed(addImplicitFinal: false)
sealed class YourClass with _$YourClass {
@Entity(realClass: YourClass)
factory YourClass({
@Default(0) @Id() int localId,
@Unique() String? id,
}) = _YourClass;
} |
In #229, the example supporting freezed entities uses assignable ids. Since freezed demands entities to be immutable, attempts to use value 0 ids fail in
objectbox-dart/objectbox/lib/src/native/box.dart
Line 222 in e587529
Is it possible to use non-assignable ids with immutable entities currently? If not, would it be beneficial to make it so, e.g. by modifying the code above to use a copyWith or some other mechanism to set newly assigned ids?
The text was updated successfully, but these errors were encountered: