isar / isar/hive

Can we reduce number of lines in registering multiple adapters for nested models

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

Description

**Question**
I am trying to save a nested model in the hive and am registering multiple adapters for each object/model. Is there any simplest way to perform such action?

Can we reduce these multiple lines of code as all are depending on each other like:
```dart
class AlbumData extends HiveObject {
//example
@HiveField(0)
String? _listeners;
@HiveField(1)
String? _playcount;
@HiveField(2)
Wiki? _wiki;
@HiveField(3)
Tracks? _tracks;
@HiveField(4)
List? _image;
@HiveField(5)
Tags? _tags;
}
```
**Code sample**
```dart
// during initialization, I have to register multiple adapters
//otherwise can't save object
await Hive.initFlutter();
Hive
..registerAdapter(AlbumDataAdapter())
..registerAdapter(WikiAdapter())
..registerAdapter(TracksAdapter())
..registerAdapter(TrackAdapter())
..registerAdapter(TagsAdapter())
..registerAdapter(TagAdapter())
..registerAdapter(ArtistDetailAdapter())
..registerAdapter(StreamableAdapter())
..registerAdapter(ImageAdapter());

await Hive.openBox(musicAlbumBoxName);
```

**Expectation**
As `AlbumData` is already extended with `HiveObject` so I am expecting something like this:
```dart
await Hive.initFlutter();
Hive
..registerAdapter(AlbumDataAdapter());
//Rest of the adapters should automatically attached
await Hive.openBox(musicAlbumBoxName);
```

**Version**
- Platform: iOS, Android, Mac, Windows, Linux, Web
- Flutter version: 2.2.3
- Hive version: 2.0.4

**Libraries:**
```dart
dependencies:
hive: ^2.0.4
hive_flutter: ^1.1.0

dev_dependencies:
build_runner: ^2.0.6
hive_generator: ^1.1.0
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the initialization flow in the issue: Hive.initFlutter(), the chained registerAdapter calls, and Hive.openBox(). Review how generated adapters for AlbumData and its nested models are registered, then determine whether automatic registration is feasible. Done means the requested single-adapter registration behavior is implemented or its limitations are clearly documented.

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
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.