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

flutter hive: Unhandled Exception: RangeError: Not enough bytes available #498

Closed
tazik561 opened this issue Nov 24, 2020 · 3 comments
Closed
Labels
question Further information is requested

Comments

@tazik561
Copy link

In my flutter application I have used hive to persist my data.Before i use my custom adapter like this:

class MessageModelAdapter
    extends TypeAdapter<GetPushNotificationContentListResponse> {
  @override
  final typeId = 4;

  @override
  GetPushNotificationContentListResponse read(BinaryReader reader) {
    var numOfFields = reader.readByte();
    var fields = <int, dynamic>{
      for (var i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
    };
    return GetPushNotificationContentListResponse(
      message: fields[0],
      messageId: fields[1],
      notificationType: fields[2],
      pushId: fields[3],
      sendDate: fields[4],
    );
  }

  @override
  void write(BinaryWriter writer, GetPushNotificationContentListResponse obj) {
    writer
      ..writeByte(5)
      ..writeByte(0)
      ..write(obj.message)
      ..writeByte(1)
      ..write(obj.messageId)
      ..writeByte(2)
      ..write(obj.notificationType)
      ..writeByte(3)
      ..write(obj.pushId)
      ..writeByte(4)
      ..write(obj.sendDate);
  }
}

But now i am trying to use like this:

class MessageModelAdapter
    extends TypeAdapter<GetPushNotificationContentListResponse> {
  @override
  final typeId = 4;

  @override
  GetPushNotificationContentListResponse read(BinaryReader reader) {

    return GetPushNotificationContentListResponse()
      ..message = reader.read()
      ..messageId = reader.read()
      ..notificationType = reader.read()
      ..pushId = reader.read()
      ..sendDate = reader.read();
  }

  @override
  void write(BinaryWriter writer, GetPushNotificationContentListResponse obj) {
    writer
      ..write(obj.message)
      ..write(obj.messageId)
      ..write(obj.notificationType)
      ..write(obj.pushId)
      ..write(obj.sendDate);
  }
}

But when i run application i got this error:

E/flutter ( 6361): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: RangeError: Not enough bytes available.
E/flutter ( 6361): #0      BinaryReaderImpl._requireBytes
package:hive/…/binary/binary_reader_impl.dart:49
E/flutter ( 6361): #1      BinaryReaderImpl.readByteList
package:hive/…/binary/binary_reader_impl.dart:142
E/flutter ( 6361): #2      BinaryReaderImpl.read
package:hive/…/binary/binary_reader_impl.dart:304
E/flutter ( 6361): #3      MessageModelAdapter.read
package:iparsian_flutter/…/database/push_message_box.dart:150
E/flutter ( 6361): #4      BinaryReaderImpl.read
package:hive/…/binary/binary_reader_impl.dart:325
E/flutter ( 6361): #5      BinaryReaderImpl.readFrame
package:hive/…/binary/binary_reader_impl.dart:273
E/flutter ( 6361): #6      FrameHelper.framesFromBytes
package:hive/…/binary/frame_helper.dart:17
E/flutter ( 6361): #7      FrameIoHelper.framesFromFile
package:hive/…/io/frame_io_helper.dart:41
E/flutter ( 6361): <asynchronous suspension>
E/flutter ( 6361): #8      StorageBackendVm.initialize
package:hive/…/vm/storage_backend_vm.dart:82
E/flutter ( 6361): <asynchronous suspension>
E/flutter ( 6361): #9      BoxBaseImpl.initialize
package:hive/…/box/box_base_impl.dart:90
E/flutter ( 6361): #10     HiveImpl._openBox
package:hive/src/hive_impl.dart:106

How can i change implementation of adapter without problem?

@tazik561 tazik561 added the question Further information is requested label Nov 24, 2020
@themisir
Copy link
Contributor

Duplicate of #424

@themisir themisir marked this as a duplicate of #424 Nov 24, 2020
@tazik561
Copy link
Author

I don't want to reinstall the application. I can not force the user to reinstall application .user has a data and they don't want to deleted.
There is no way to changing model structure without reinstall app?

@musman-deve
Copy link

musman-deve commented Dec 28, 2022

If you have made any changes in Adapter then try re-generating it. Your issue will be fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants