isar / isar/hive

Get a list of child objects

Open
#925 0 comments 1 reaction 0 assignees View on GitHub
question
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

Hello, I have the doubt of how to obtain a list of child objects of the box, I have the following classes:

```dart
//parent
@HiveType(typeId: 2)
class Message
{
@HiveField(0)
int id;

@HiveField(1)
String content;

@HiveField(2)
String type;

@HiveField(3)
DateTime dateTime;

Message(this.id, this.content, this.type, this.dateTime);
}

//child1
@HiveType(typeId: 3)
class MessageFromCurrentUser extends Message
{
@HiveField(4)
late String status; //waiting - server - client

MessageFromCurrentUser(int id, String content, String type, DateTime dateTime, {String status = "waiting"}) :
super(id, content, type, dateTime)
{
this.status = status;
}
}

//child2

@HiveType(typeId: 4)
class MessageFromContraryUser extends Message
{
@HiveField(4)
late bool hasBeenRead;

MessageFromContraryUser(int id, String content, String type, DateTime dateTime, {bool hasBeenRead = false}) :
super(id, content, type, dateTime)
{
this.hasBeenRead = hasBeenRead;
}
}
```

I can open the box and cast the objects without problems to the parent class, but clearly by doing this I lose their true classes. For now I save the objects in map type, doing the necessary conversions, clearly this is not very optimal because I will save a lot of data. Is there any way to optimize or fix this situation?

**Version**
- Hive version: [^2.0.3]

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the Message, MessageFromCurrentUser, and MessageFromContraryUser definitions and the code that opens and reads the Hive box. Determine whether Hive 2.0.3 preserves the concrete child type when values are read through the parent type, and identify the supported way to retrieve those child objects without map conversions. Done means the behavior or limitation is verified and clearly documented with a reproducible example.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.