objectbox / objectbox/objectbox-dart
Freezed (immutable) support with 0 id fails due to assignment to final id
Nobody has claimed this yet.
- Dominant language
- Dart
- Stars
- 1.2k
- Forks
- 162
- Avg merge
- 15m
- Merged PRs (30d)
- 1
Description
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 https://github.com/objectbox/objectbox-dart/blob/e587529f3a32ce7769e9a53a24ea4afafdd6d970/objectbox/lib/src/native/box.dart#L222 with error
Unhandled Exception: Invalid argument(s): Field Model.id is read-only (final or getter-only) and it was declared to be self-assigned. However, the currently inserted object (.id=0) doesn't match the inserted ID (ID 1). You must assign an ID before calling [box.put()].
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?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with objectbox/lib/src/native/box.dart around line 222 and compare the immutable Freezed example from issue #229. Reproduce the failure with an entity whose id is 0 and is final, then determine whether immutable entities can receive newly assigned IDs without mutation. Done means the supported behavior and its tests clearly cover this case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100