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

RangeError on openBox() after put() #64

Closed
andrei-pavel opened this issue Oct 1, 2019 · 1 comment
Closed

RangeError on openBox() after put() #64

andrei-pavel opened this issue Oct 1, 2019 · 1 comment
Assignees
Labels
duplicate This issue or pull request already exists

Comments

@andrei-pavel
Copy link

Although I have annotations, I haven't used hive_generator, because documentation is missing? I've found some information online about running build_runner which mentions some entry.g.dart file while it runs, but doesn't generate it? So I've written the adapter manually.

Steps to Reproduce
put() a list of entries
openBox() that has entries
Receive RangeError:

E/flutter (31284): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: RangeError: Not enough bytes available.
E/flutter (31284): #0      BinaryReaderImpl._requireBytes (package:hive/src/binary/binary_reader_impl.dart:40)
E/flutter (31284): #1      BinaryReaderImpl.readByte (package:hive/src/binary/binary_reader_impl.dart:52)
E/flutter (31284): #2      BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:211)
E/flutter (31284): #3      BinaryReaderImpl.readList (package:hive/src/binary/binary_reader_impl.dart:192)
E/flutter (31284): #4      BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:236)
E/flutter (31284): #5      Frame.decodeValue (package:hive/src/binary/frame.dart:89)
E/flutter (31284): #6      Frame.decode (package:hive/src/binary/frame.dart:78)
E/flutter (31284): #7      FrameIoHelper.framesFromFile (package:hive/src/io/frame_io_helper.dart:77)
E/flutter (31284): <asynchronous suspension>
E/flutter (31284): #8      StorageBackendVm.initialize (package:hive/src/backend/storage_backend_vm.dart:85)
E/flutter (31284): <asynchronous suspension>
E/flutter (31284): #9      BoxBase.initialize (package:hive/src/box/box_base.dart:87)
E/flutter (31284): #10     HiveImpl.openBoxInternal (package:hive/src/hive_impl.dart:92)
E/flutter (31284): <asynchronous suspension>
E/flutter (31284): #11     HiveImpl.openBox (package:hive/src/hive_impl.dart:72)
E/flutter (31284): <asynchronous suspension>
E/flutter (31284): #12     openBox.<anonymous closure>

Code sample

@HiveType()
class Entry {
  @HiveField(0)
  String title;
  @HiveField(1)
  String url;
  @HiveField(2)
  Entry next;
  @HiveField(3)
  List<Entry> children = List();

  Entry(this.title, this.url);

  static initializeHive() {
    Hive.registerAdapter(EntryAdapter(), 0);
  }

  get urlExists => url != null && url.isNotEmpty;
}

class EntryAdapter extends TypeAdapter<Entry> {
  @override
  Entry read(BinaryReader reader) {
    Entry e;
    try {
      String title = reader.readString();
      String url = reader.readString();
      e = Entry(title, url);
      bool isNext = reader.readBool();
      if (isNext) {
        Entry x = read(reader);
        if (x != null) {
          e.next = x;
        }
      }
      var length = reader.readUint32();
      for (var i = 0; i < length; ++i) {
        bool b = reader.readBool();
        if (b) {
          Entry x = read(reader);
          if (x != null) {
            e.children.add(x);
          }
        }
      }
      return e;
    } catch (exception) {
      return null;
    }
  }

  @override
  void write(BinaryWriter writer, Entry e) {
    if (e == null || !e.urlExists) {
      return;
    }
    writer.writeString(e.title);
    writer.writeString(e.url);
    if (e.next == null || !e.urlExists) {
      writer.writeBool(false);
    } else {
      writer.writeBool(true);
      write(writer, e.next);
    }
    writer.writeUint32(e.children.length);
    for (var child in e.children) {
      if (child == null || !child.urlExists) {
        writer.writeBool(false);
      } else {
        writer.writeBool(true);
        write(writer, child);
      }
    }
  }
}

**Version**
 - Platform: Android
 - Flutter version: 1.10.7-pre.99
 - Hive version: 1.0.0
@andrei-pavel andrei-pavel added the bug Something isn't working label Oct 1, 2019
@simc
Copy link
Member

simc commented Oct 1, 2019

Duplicate of #63. You can find the solution there. I hope the dart team fixes the issue soon.

@simc simc added duplicate This issue or pull request already exists and removed bug Something isn't working labels Oct 1, 2019
@simc simc closed this as completed Oct 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants