isar / isar/hive

first register got lost

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

Description

I was debugging another problem when I found this odd situation, steps to reproduce
I am using linux, so
1) I created a folder called hive_program and added the following files on it

pubspec.yaml:
```
name: hive_bug

environment:
sdk: '>=2.14.0 <3.0.0'

dependencies:
hive: ^2.2.3
```

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

const boxname = "persons";
Box getBox() {
return Hive.box(boxname);
}

void printData() {
for (var p in getBox().values) {
print(p.data());
}
}

void main() async {
Hive.init("/home/lucas");
Hive.registerAdapter(PersonAdapter());
await Hive.openBox(boxname);

getBox().clear();
Person person1 = Person("John", "1");

Person person2 = Person("Sara", "2");

Person person3 = Person("Lucas", "3");
await getBox().add(person1);
await getBox().add(person2);
await getBox().add(person3);
printData();

await getBox().delete(1);

print("Sara record deleted");
printData();
}
```

model.dart:
```
import 'package:hive/hive.dart';
part 'model.g.dart';

@HiveType(typeId: 1)
class Person extends HiveObject {
@HiveField(1)
String name;

@HiveField(2)
String phone;
Person(this.name,this.phone);

Map data(){
return {
"name":name,
"phone":phone,
};
}
}
```

model.g.dart:
```
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'model.dart';

// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************

class PersonAdapter extends TypeAdapter {
@override
final int typeId = 1;

@override
Person read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = {
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return Person(
fields[1] as String,
fields[2] as String,
);
}

@override
void write(BinaryWriter writer, Person obj) {
writer
..writeByte(2)
..writeByte(1)
..write(obj.name)
..writeByte(2)
..write(obj.phone);
}

@override
int get hashCode => typeId.hashCode;

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is PersonAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}
```

2)$ cd hive_program

3)$ dart pub get

4)$dart run main.dart
Ouput:
```
{name: Sara, phone: 2}
{name: Lucas, phone: 3}
Sara record deleted
{name: Lucas, phone: 3}
```

A reminder of main code

```
Hive.init("/home/lucas");
Hive.registerAdapter(PersonAdapter());
await Hive.openBox(boxname);

getBox().clear();
Person person1 = Person("John", "1");

Person person2 = Person("Sara", "2");

Person person3 = Person("Lucas", "3");
await getBox().add(person1);
await getBox().add(person2);
await getBox().add(person3);
printData();

await getBox().delete(1);

print("Sara record deleted");
printData();
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the reproduction in main.dart and the Person adapter in model.dart, then run dart pub get and dart run main.dart. Trace the box operations around clearing, adding, and deleting records; done means the first record is retained before deletion and the output matches the intended record sequence.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.