isar / isar/hive

After updating HiveObject its key will become null

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

Description

I think it's a bug, why is the key changing to null if it's must be constant? But I want to say that the key is not changing internally in the hive box, it's still the same, but HiveObject for some reason holds null after the update, in order to fix this I need to hot restart my app.

Reproducible code example with dependencies is provided below:

```dart
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';

part 'main.g.dart';

// dependencies:
// hive: ^2.0.5
// hive_flutter: ^1.1.0
// path_provider: ^2.0.8
// build_runner: ^2.1.7
// hive_generator: ^1.1.2

void main() async {
WidgetsFlutterBinding.ensureInitialized();

final directory = await getApplicationDocumentsDirectory();

Hive.init(directory.path);

Hive.registerAdapter(MyModelAdapter());

await Hive.openBox('my_model');

Hive.box('my_model').add(MyModel('Test'));

runApp(const MyApp());
}

class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);

@override
State createState() => _MyAppState();
}

class _MyAppState extends State {
late MyModel test;

@override
void initState() {
test = Hive.box('my_model').values.first;
super.initState();
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(),
// 2. Check how key will become null
// Accepted behaviour: key must not be null
Text(
'Model ${test.name} Key: ${test.key}',
),
// 1. Tap this button
TextButton(
onPressed: () {
Hive.box('my_model').put(
test.key,
MyModel('Test ${DateTime.now().second}'),
);
setState(() {});
},
child: const Text('update'),
),
],
),
),
);
}
}

@HiveType(typeId: 0)
class MyModel extends HiveObject {
MyModel(this.name);

@HiveField(0)
final String name;
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the provided Dart reproduction and trace the interaction between HiveObject.key and the box.put call that replaces the model. Confirm the key remains non-null after the update, and add or run a regression test covering this replacement behavior if the repository has a relevant test entry point.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.