Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HiveObject does not have key when loaded lazy #112

Closed
PauloMelo opened this issue Nov 1, 2019 · 2 comments
Assignees
Labels
bug
Milestone

Comments

@PauloMelo
Copy link

@PauloMelo PauloMelo commented Nov 1, 2019

Steps to Reproduce
When opening a box in lazy mode the key of the HiveObject is not set, as it is when the object is opened normally.

In the code Sample bellow it shows:
0 1
null 2

Code sample

import 'dart:io';

import 'package:hive/hive.dart';

@HiveType()
class Test extends HiveObject {
  @HiveField(0)
  final int id;

  Test (this.id);
}

class TestAdapter extends TypeAdapter<Test> {
  @override
  Test read(BinaryReader reader) {
    var numOfFields = reader.readByte();
    var fields = <int, dynamic>{
      for (var i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
    };
    return Test(
      fields[0] as int,
    );
  }

  @override
  void write(BinaryWriter writer, Test obj) {
    writer
      ..writeByte(1)
      ..writeByte(0)
      ..write(obj.id);
  }
}

void main() async {
    Hive.init(Directory.current.path);
    Hive.registerAdapter(TestAdapter(), 0);

    var memBox = await Hive.openBox('normalBox');
    await memBox.add(Test(1));

    var lazyBox = await Hive.openBox('lazyBox', lazy: true) as LazyBox;
    await lazyBox.add(Test(2));

    var obj1 = memBox.getAt(0);
    print("${obj1.key} ${obj1.id}");

    var obj2 = await lazyBox.getAt(0);
    print("${obj2.key} ${obj2.id}");
}

Version

  • Platform: iOS, Android, Mac
  • Flutter version: 1.10.15
  • Hive version: 1.1.1
@leisim

This comment has been minimized.

Copy link
Member

@leisim leisim commented Nov 1, 2019

Thanks for reporting!

@leisim leisim closed this in b234dec Nov 4, 2019
@leisim

This comment has been minimized.

Copy link
Member

@leisim leisim commented Nov 4, 2019

Fix will be published with v1.2.0

@leisim leisim added this to the 1.2.0 milestone Nov 4, 2019
leisim added a commit that referenced this issue Nov 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.