Hive: Unhandled Exception: RangeError: Not enough bytes available.

Created on 6 Sep 2020  路  19Comments  路  Source: hivedb/hive

Restarted application in 6,661ms.
W/DynamiteModule(20626): Local module descriptor class for providerinstaller not found.
I/DynamiteModule(20626): Considering local module providerinstaller:0 and remote module providerinstaller:0
W/ProviderInstaller(20626): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
W/ConnectivityManager.CallbackHandler(20626): callback not found for CALLBACK_AVAILABLE message
E/flutter (20626): [ERROR:flutter/lib/ui/ui_dart_state.cc(171)] Unhandled Exception: RangeError: Not enough bytes available.
E/flutter (20626): #0      BinaryReaderImpl._requireBytes
E/flutter (20626): #1      BinaryReaderImpl.readByte
E/flutter (20626): #2      StaticModelAdapter.read
E/flutter (20626): #3      BinaryReaderImpl.read
E/flutter (20626): #4      BinaryReaderImpl.readFrame
E/flutter (20626): #5      FrameHelper.framesFromBytes
E/flutter (20626): #6      FrameIoHelper.framesFromFile
E/flutter (20626): <asynchronous suspension>
E/flutter (20626): #7      StorageBackendVm.initialize
E/flutter (20626): <asynchronous suspension>
E/flutter (20626): #8      BoxBaseImpl.initialize
E/flutter (20626): #9      HiveImpl._openBox
E/flutter (20626): <asynchronous suspension>
E/flutter (20626): #10     HiveImpl.openBox
E/flutter (20626): #11     main
E/flutter (20626): <asynchronous suspension>
E/flutter (20626): #12     _runMainZoned.<anonymous closure>.<anonymous closure>  (dart:ui/hooks.dart:231:25)
E/flutter (20626): #13     _rootRun  (dart:async/zone.dart:1190:13)
E/flutter (20626): #14     _CustomZone.run  (dart:async/zone.dart:1093:19)
E/flutter (20626): #15     _runZoned  (dart:async/zone.dart:1630:10)
E/flutter (20626): #16     runZonedGuarded  (dart:async/zone.dart:1618:12)
E/flutter (20626): #17     _runMainZoned.<anonymous closure>  (dart:ui/hooks.dart:223:5)
E/flutter (20626): #18     _startIsolate.<anonymous closure>  (dart:isolate-patch/isolate_patch.dart:301:19)
E/flutter (20626): #19     _RawReceivePortImpl._handleMessage  (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter (20626):
E/flutter (20626): [ERROR:flutter/lib/ui/ui_dart_state.cc(171)] Unhandled Exception: RangeError: Not enough bytes available.
E/flutter (20626): #0      BinaryReaderImpl._requireBytes
E/flutter (20626): #1      BinaryReaderImpl.readByte
E/flutter (20626): #2      StaticModelAdapter.read
E/flutter (20626): #3      BinaryReaderImpl.read
E/flutter (20626): #4      BinaryReaderImpl.readFrame
E/flutter (20626): #5      FrameHelper.framesFromBytes
E/flutter (20626): #6      FrameIoHelper.framesFromFile
E/flutter (20626): <asynchronous suspension>
E/flutter (20626): #7      StorageBackendVm.initialize
E/flutter (20626): <asynchronous suspension>
E/flutter (20626): #8      BoxBaseImpl.initialize
E/flutter (20626): #9      HiveImpl._openBox
E/flutter (20626): <asynchronous suspension>
E/flutter (20626): #10     HiveImpl.openBox
E/flutter (20626): #11     main
E/flutter (20626): <asynchronous suspension>
E/flutter (20626): #12     _runMainZoned.<anonymous closure>.<anonymous closure>  (dart:ui/hooks.dart:231:25)
E/flutter (20626): #13     _rootRun  (dart:async/zone.dart:1190:13)
E/flutter (20626): #14     _CustomZone.run  (dart:async/zone.dart:1093:19)
E/flutter (20626): #15     _runZoned  (dart:async/zone.dart:1630:10)
E/flutter (20626): #16     runZonedGuarded  (dart:async/zone.dart:1618:12)
E/flutter (20626): #17     _runMainZoned.<anonymous closure>  (dart:ui/hooks.dart:223:5)
E/flutter (20626): #18     _startIsolate.<anonymous closure>  (dart:isolate-patch/isolate_patch.dart:301:19)
E/flutter (20626): #19     _RawReceivePortImpl._handleMessage  (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter (20626):

problem

Most helpful comment

nope, it is working fine when i start debugging. after i restart 2-3 time. this error happens

All 19 comments

Could you reproduce the issue?

this issue is solved when i delete local data.

Again Facing this issue in flutter web. Can't able to resolve it.

Restarted application in 648ms.
Prest :::::: 123871653
Error: RangeError: Not enough bytes available.
    at Object.throw_ [as throw] (http://localhost:64762/dart_sdk.js:4332:11)
    at binary_reader_impl.BinaryReaderImpl.new.[_requireBytes] (http://localhost:64762/packages/hive/src/box/lazy_box_impl.dart.lib.js:1243:19)
    at binary_reader_impl.BinaryReaderImpl.new.readByte (http://localhost:64762/packages/hive/src/box/lazy_box_impl.dart.lib.js:1252:26)
    at http://localhost:64762/packages/website/Models/User_model.dart.lib.js:437:28
    at User_model.UserDataAdapter.new.read (http://localhost:64762/packages/website/Models/User_model.dart.lib.js:439:9)
    at binary_reader_impl.BinaryReaderImpl.new.read (http://localhost:64762/packages/hive/src/box/lazy_box_impl.dart.lib.js:1501:35)
    at storage_backend_js.StorageBackendJs.new.decodeValue (http://localhost:64762/packages/hive/src/box/lazy_box_impl.dart.lib.js:1628:27)
    at MappedListIterable.new.elementAt (http://localhost:64762/dart_sdk.js:21547:25)
    at ListIterator.new.moveNext (http://localhost:64762/dart_sdk.js:21341:55)
    at JsIterator.next (http://localhost:64762/dart_sdk.js:5999:21)
    at storage_backend_js.StorageBackendJs.new.initialize (http://localhost:64762/packages/hive/src/box/lazy_box_impl.dart.lib.js:1685:20)
    at initialize.next (<anonymous>)
    at http://localhost:64762/dart_sdk.js:37592:33
    at _RootZone.runUnary (http://localhost:64762/dart_sdk.js:37446:58)
    at _FutureListener.thenAwait.handleValue (http://localhost:64762/dart_sdk.js:32430:29)
    at handleValueCallback (http://localhost:64762/dart_sdk.js:32977:49)
    at Function._propagateToListeners (http://localhost:64762/dart_sdk.js:33015:17)
    at _Future.new.[_completeWithValue] (http://localhost:64762/dart_sdk.js:32858:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:64762/dart_sdk.js:32880:35)
    at Object._microtaskLoop (http://localhost:64762/dart_sdk.js:37707:13)
    at _startMicrotaskLoop (http://localhost:64762/dart_sdk.js:37713:13)
    at http://localhost:64762/dart_sdk.js:33232:9
Error: RangeError: Not enough bytes available.
    at Object.throw_ [as throw] (http://localhost:64762/dart_sdk.js:4332:11)
    at binary_reader_impl.BinaryReaderImpl.new.[_requireBytes] (http://localhost:64762/packages/hive/src/box/lazy_box_impl.dart.lib.js:1243:19)
    at binary_reader_impl.BinaryReaderImpl.new.readByte (http://localhost:64762/packages/hive/src/box/lazy_box_impl.dart.lib.js:1252:26)
    at http://localhost:64762/packages/website/Models/User_model.dart.lib.js:437:28
    at User_model.UserDataAdapter.new.read (http://localhost:64762/packages/website/Models/User_model.dart.lib.js:439:9)
    at binary_reader_impl.BinaryReaderImpl.new.read (http://localhost:64762/packages/hive/src/box/lazy_box_impl.dart.lib.js:1501:35)
    at storage_backend_js.StorageBackendJs.new.decodeValue (http://localhost:64762/packages/hive/src/box/lazy_box_impl.dart.lib.js:1628:27)
    at MappedListIterable.new.elementAt (http://localhost:64762/dart_sdk.js:21547:25)
    at ListIterator.new.moveNext (http://localhost:64762/dart_sdk.js:21341:55)
    at JsIterator.next (http://localhost:64762/dart_sdk.js:5999:21)
    at storage_backend_js.StorageBackendJs.new.initialize (http://localhost:64762/packages/hive/src/box/lazy_box_impl.dart.lib.js:1685:20)
    at initialize.next (<anonymous>)
    at http://localhost:64762/dart_sdk.js:37592:33
    at _RootZone.runUnary (http://localhost:64762/dart_sdk.js:37446:58)
    at _FutureListener.thenAwait.handleValue (http://localhost:64762/dart_sdk.js:32430:29)
    at handleValueCallback (http://localhost:64762/dart_sdk.js:32977:49)
    at Function._propagateToListeners (http://localhost:64762/dart_sdk.js:33015:17)
    at _Future.new.[_completeWithValue] (http://localhost:64762/dart_sdk.js:32858:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:64762/dart_sdk.js:32880:35)
    at Object._microtaskLoop (http://localhost:64762/dart_sdk.js:37707:13)
    at _startMicrotaskLoop (http://localhost:64762/dart_sdk.js:37713:13)
    at http://localhost:64762/dart_sdk.js:33232:9

Did the issue happened after changing model structure or randomly?

nope, it is working fine when i start debugging. after i restart 2-3 time. this error happens

First application start is ok, but second and all the next causes crash

First application start is ok, but second and all the next causes crash

Could you share code with us to identify cause?

when i clear all cache & storage and run builder again this issue is resolved

I call await StorageHive().init(); before main app class, for the first time is everything ok, but while opening app the second. etc times always error will be. I have to wipe emulator, to reinstall app on real device but problem still exists. Versions of hive and generator are the last from repo's.

Method invokation
Place where error occurs
Link to Image error

[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: RangeError: Not enough bytes available.

any find the problem

flutterhive

For several days I suffered with the same problem and could not understand why it appears. Changed package versions and much more.

Everything turned out to be trite.
I save the array of enams to the database.
I specified the type for enam (@HiveType), but did not specify the indices for the values (@HiveField), and this led to the fact that everything was successfully written to the database, but not read upon restarting!

This fixed my problem.

I figured it (atleast in my case).

This was my code:

  SessionTokenInfo read(BinaryReader reader) {
    print("read ${reader.readString()}");
    return SessionTokenInfo.fromJson(jsonDecode(reader.readString()));
  }

I was reading from the same BinaryReader twice. I assume there's a finite buffer that gets cleared as you read. Calling read twice (apparently) was the problem.

I hope this helps someone else.

I figured it (atleast in my case).

This was my code:

  SessionTokenInfo read(BinaryReader reader) {
    print("read ${reader.readString()}");
    return SessionTokenInfo.fromJson(jsonDecode(reader.readString()));
  }

I was reading from the same BinaryReader twice. I assume there's a finite buffer that gets cleared as you read. Calling read twice (apparently) was the problem.

I hope this helps someone else.

reader.readString() moves cursor to end of the read data. So in your case second readString() will throw an error because there might be nothing available to read (Even if there's data, the returned data will not be same as you've printed).

Instead of reading twice you can store value in variable then use it twice.

  SessionTokenInfo read(BinaryReader reader) {
    final str = reader.readString();
    print('read $str');
    return SessionTokenInfo.fromJson(jsonDecode(str));
  }

i occur the same problem,i did know what went wrong?

@HumesFork please proof-read your comment. The previous comment explain the issue clearly.

@leisim Maybe improve the error message? Seems a decent number of devs all find it unclear.

@cosmotek that's not so easy because there are a lot of reasons why this error may happen. Do you have a suggestion?

facing this issue using firestore. when i store firestore data locally and then restart my app. i get the following error -

[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: RangeError: Not enough bytes available.

0 BinaryReaderImpl._requireBytes (package:hive/src/binary/binary_reader_impl.dart:49:7)

1 BinaryReaderImpl.readByte (package:hive/src/binary/binary_reader_impl.dart:65:5)

2 UserFSDataAdapter.read (package:fluent_life_flutter/db/model/user_model.g.dart:17:52)

3 BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:325:33)

4 BinaryReaderImpl.readFrame (package:hive/src/binary/binary_reader_impl.dart:273:26)

5 FrameHelper.framesFromBytes (package:hive/src/binary/frame_helper.dart:17:26)

6 FrameIoHelper.framesFromFile (package:hive/src/io/frame_io_helper.dart:41:12)

7 StorageBackendVm.initialize (package:hive/src/backend/vm/storage_backend_vm.dart:82:30)

8 BoxBaseImpl.initialize (package:hive/src/box/box_base_impl.dart:90:20)

9 HiveImpl._openBox (package:hive/src/hive_impl.dart:106:22)

10 HiveImpl.openBox (package:hive/src/hive_impl.dart:135:18)

11 HiveProvider.openBox (package:fluent_life_flutter/db/hive_provider.dart:26:16)

12 main (package:fluent_life_flutter/main.dart:15:22)

13 _runMainZoned.. (dart:ui/hooks.dart:231:25)

14 _rootRun (dart:async/zone.dart:1190:13)

15 _CustomZone.run (dart:async/zone.dart:1093:19)

16 _runZoned (dart:async/zone.dart:1630:10)

17 runZonedGuarded (dart:async/zone.dart:1618:12)

18 _runMainZoned. (dart:ui/hooks.dart:223:5)

19 _startIsolate. (dart:isolate-patch/isolate_patch.dart:301:19)

20 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: RangeError: Not enough bytes available.

0 BinaryReaderImpl._requireBytes (package:hive/src/binary/binary_reader_impl.dart:49:7)

1 BinaryReaderImpl.readByte (package:hive/src/binary/binary_reader_impl.dart:65:5)

2 UserFSDataAdapter.read (package:fluent_life_flutter/db/model/user_model.g.dart:17:52)

3 BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:325:33)

4 BinaryReaderImpl.readFrame (package:hive/src/binary/binary_reader_impl.dart:273:26)

5 FrameHelper.framesFromBytes (package:hive/src/binary/frame_helper.dart:17:26)

6 FrameIoHelper.framesFromFile (package:hive/src/io/frame_io_helper.dart:41:12)

7 StorageBackendVm.initialize (package:hive/src/backend/vm/storage_backend_vm.dart:82:30)

8 BoxBaseImpl.initialize (package:hive/src/box/box_base_impl.dart:90:20)

9 HiveImpl._openBox (package:hive/src/hive_impl.dart:106:22)

10 HiveImpl.openBox (package:hive/src/hive_impl.dart:135:18)

11 HiveProvider.openBox (package:fluent_life_flutter/db/hive_provider.dart:26:16)

12 main (package:fluent_life_flutter/main.dart:15:22)

13 _runMainZoned.. (dart:ui/hooks.dart:231:25)

14 _rootRun (dart:async/zone.dart:1190:13)

15 _CustomZone.run (dart:async/zone.dart:1093:19)

16 _runZoned (dart:async/zone.dart:1630:10)

17 runZonedGuarded (dart:async/zone.dart:1618:12)

18 _runMainZoned. (dart:ui/hooks.dart:223:5)

19 _startIsolate. (dart:isolate-patch/isolate_patch.dart:301:19)

20 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

I had this exact same issue, in my case I created an adapter for the flutter math Point class which I used inside one of my business logic classes.

The adapter I first created was like this:

class PointAdapter extends TypeAdapter<Point> {
  @override
  final typeId = HiveUtils.POINT_TYPE_ID;

  @override
  Point read(BinaryReader reader) => Point(reader.read(0), reader.read(1));

  @override
  void write(BinaryWriter writer, Point obj) {
    writer
      ..writeByte(2)
      ..writeByte(0)
      ..write(obj.x)
      ..writeByte(1)
      ..write(obj.y);
  }
}

But the Point class is a flutter framework class and doesn't have @HiveField set for 'x' and 'y'... so the first time the app runs everything gets stored fine but after a hot restart the reader throws the RangeError exception.

Then I updated the adapter to this:

class PointAdapter extends TypeAdapter<Point> {
  @override
  final typeId = HiveUtils.POINT_TYPE_ID;

  @override
  Point read(BinaryReader reader) => Point(reader.readInt(), reader.readInt());

  @override
  void write(BinaryWriter writer, Point obj) {
    writer
      ..writeInt(obj.x)
      ..writeInt(obj.y);
  }
}

and everything works fine now.

I had the same issue. But the name of my hive box was about 30 characters long. I don't remember what's the length exactly.

I had changed the name to make it shorter and it helped

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abacaj picture abacaj  路  3Comments

Ferdzzzzzzzz picture Ferdzzzzzzzz  路  3Comments

azilvl picture azilvl  路  3Comments

kaboc picture kaboc  路  3Comments

NourEldinShobier picture NourEldinShobier  路  3Comments