isar / isar/hive

HiveList and ValueListenableBuilder in Flutter

Open
#449 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**
Whenever I update a HiveList I expect it to propagate notifications to all its dependants and listenables but it doesn't seem to work.
Though database itself is updated correctly (just no notifications).
When I delete an item from HiveList, it doesn't trigger updates in Flutter (via listenable).
The same happens when I add/modify an item, but that I overcame by using `deck.save()`.
I can probably call `deck.save()` also for deletes but I think that the main advantage of HiveList is not to worry about dependants.

**Code sample**

Code is overly simplified to not blow it up.

my models:

```dart
import 'package:hive/hive.dart';

part 'deck.g.dart';

@HiveType(typeId: 1)
class Card extends HiveObject {
@HiveField(0)
String front;

@HiveField(1)
String back;
}

@HiveType(typeId: 0)
class Deck extends HiveObject {
@HiveField(0)
String title;

@HiveField(1)
HiveList cards;
}
```
boxes initialization (I store cards and decks in different boxes):
```
Hive.registerAdapter(DeckAdapter());
Hive.registerAdapter(CardAdapter());
await Hive.openBox("cards");
await Hive.openBox("decks");
```

flutter code piece:

```dart
class CardList extends StatelessWidget {
final Deck deck;

const CardList({Key key, this.deck}): super(key: key);

@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: Hive.box("decks").listenable(keys: [deck.key]),
builder: (context, decks, child) {
return GridView.count(
children: (deck.cards ?? List()).asMap().entries.map(
(entry) => CardListItem(key: UniqueKey(), card: entry.value)
).toList(),
);
},
);
}
}
```
card item piece of code:
```dart
class CardListItem extends StatelessWidget {
final decks.Card card;

const CardListItem({Key key, this.card}) : super(key: key);

@override
Widget build(BuildContext context) {
// there is some UI code and inside one of the methods I have:
card.delete();
}
}
```

**Version**
- Platform: iOS 14.0
- Flutter version: 1.20.4
- Hive version: 1.4.4
- Hive Flutter version: 0.3.1

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.