-
Notifications
You must be signed in to change notification settings - Fork 87
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
How to override == operator and hashcode of a realm model? #1202
Comments
You are right. We currently don't support that. This is second request we get for this, and it is supported by other SDKs so I feel confident that we will eventually allow this in the Flutter / Dart SDK as well. However, I cannot give any promises to when. |
It is really important for me and many others. can't do many things without overriding these. Please let us know when this feature will be live. |
Any update on this? |
Hi @santoshakil! |
I did try using equatable package, but with no luck. Same issue what I've stated multiple times before, because Realm goal is to use realm objects in app+database layer not separately (unfortunately), this creates given issue, when object preservation is not at that time point necessary.
P.S. just checked at creation, it preserves the hashCode. Will take a deeper inspection, if it will be Realm related, I will address, if not, then sorry for my mistake. |
Changing the hashCode doesn't seem correct to me - I have the following test which passes reliably: test('RealmObject.hashCode remains stable between invocations', () {
final team = Team("TeamOne");
expect(team.hashCode, team.hashCode);
});
|
I have noticed hashCode difference here: final resp = await LoadCarServerCalls().getLoadPlan(); // List<LoadPlanPointsResponse>?
final results1 = resp?.map((r) => r.asRealm).toList(); // Mapping server objects to unmanaged realm object into list
final results2 = resp?.map((r) => r.asRealm); // Mapping server objects to unmanaged realm object into iterable
print(results1?.map((e) => e.hashCode)); // (221069079, 102039447, 1026662903)
print(results1?.map((e) => e.hashCode)); // (221069079, 102039447, 1026662903)
print(results2?.map((e) => e.hashCode)); // (400072825, 510940315, 492677555)
print(results2?.map((e) => e.hashCode)); // (398519997, 973606144, 585095231)
return results1; I found that mapping toList(), it did fix the issue (for some reason), but Iterable item did change hashcode everytime I use it from Iterable |
Any update on this? @EqualsAndHashCode(of = "id") //<= I choose wich attribute to use
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
private String name;
@NotNull
private Double price;
} |
Problem
Suppose, I create a RealmModel called _Note. When I generate realm schema it creates an object named Note. Later, we will always use the Note object. But if we need to override the hashcode and == operator then what will be the further step? I've overridden these to _Note object. But it won't work. But, if I override it to the generated Note object, it works. But, modifying generated files is not recommended. here is an example:
`
@RealmModel()
class _Note {
@PrimaryKey()
Uuid? id;
String? title;
String content = '';
@OverRide
operator ==(Object other) => other is Note && id == other.id;
@OverRide
int get hashCode => id.hashCode;
}
`
Solution
No response
Alternatives
No response
How important is this improvement for you?
Dealbreaker
Feature would mainly be used with
Local Database only
The text was updated successfully, but these errors were encountered: