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

Type adapter throws not enough byte error on read after write #10

Closed
aswinmohanme opened this issue Aug 2, 2019 · 6 comments
Closed

Comments

@aswinmohanme
Copy link

I have a tasks model annotated with hive.

import 'package:hive/hive.dart';

part 'task.g.dart';

@HiveType()
class Task {
  @HiveField(0)
  String title;
  @HiveField(1)
  bool done;
  @HiveField(2)
  int id;

  Task({this.title, this.done});
}

Here is the repo that handles these tasks files

class TasksRepo {
  static const _TASKS_KEY = "tasks";
  List<Task> tasks;

  Box tasksBox;
  final Function() rebuildWidget;

  TasksRepo({this.rebuildWidget});

  openTasksBox() async {
    tasksBox = await Hive.box('tasksBox');
    tasksBox.watch().listen((e) {
      rebuildWidget();
    });
  }

  getTasks() async {
    if (tasksBox == null) {
      await openTasksBox();
    }
    tasks = await tasksBox.get(_TASKS_KEY, defaultValue: [
      Task(title: "Create new Task", done: false),
      Task(title: "Do that task", done: false),
      Task(title: "Mark task as Done", done: true),
    ]);
    return tasks;
  }

  void closeBox() async {
    await tasksBox.close();
  }

  void toggleTaskDone(Task task) async {
    tasks = await getTasks();
    var index = tasks.indexWhere((t) => t.title == task.title);
    task.done = !task.done;
    tasks[index] = task;
    tasksBox.put(_TASKS_KEY, tasks);
  }

  clearCompleted() async {
    tasks = await getTasks();
    tasks.removeWhere((task) => task.done);
    tasksBox.put(_TASKS_KEY, tasks);
  }

  void addTask(String title) async {
    tasks = await getTasks();
    tasks.insert(0, Task(done: false, title: title));
    tasksBox.put(_TASKS_KEY, tasks);
  }
}

On the first launch of the application, everything works as expected I can add tasks and clear them. But on the second launch, the application throws this error.

I/flutter ( 6440): 
E/flutter ( 6440): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: RangeError: Not enough bytes available.
E/flutter ( 6440): #0      BinaryReaderImpl._requireBytes (package:hive/src/binary/binary_reader_impl.dart:31)
E/flutter ( 6440): #1      BinaryReaderImpl.viewBytes (package:hive/src/binary/binary_reader_impl.dart:49)
E/flutter ( 6440): #2      BinaryReaderImpl.readString (package:hive/src/binary/binary_reader_impl.dart:102)
E/flutter ( 6440): #3      TaskAdapter.read (package:lessphone/app/tasks/task.g.dart:16)
E/flutter ( 6440): #4      BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:228)
E/flutter ( 6440): #5      BinaryReaderImpl.readList (package:hive/src/binary/binary_reader_impl.dart:175)
E/flutter ( 6440): #6      BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:219)
E/flutter ( 6440): #7      Frame.decodeBody (package:hive/src/binary/frame.dart:119)
E/flutter ( 6440): #8      readFramesFromFile (package:hive/src/io/frame_io_helper.dart:67)
E/flutter ( 6440): <asynchronous suspension>
E/flutter ( 6440): #9      StorageBackendVm.initialize (package:hive/src/backend/storage_backend_vm.dart:87)
E/flutter ( 6440): <asynchronous suspension>
E/flutter ( 6440): #10     BoxImpl.initialize (package:hive/src/box/box_impl.dart:88)
E/flutter ( 6440): <asynchronous suspension>
E/flutter ( 6440): #11     openBox (package:hive/src/backend/storage_backend_vm.dart:32)
E/flutter ( 6440): <asynchronous suspension>
E/flutter ( 6440): #12     HiveImpl.box (package:hive/src/hive_impl.dart:50)
E/flutter ( 6440): <asynchronous suspension>
E/flutter ( 6440): #13     TasksRepo.openTasksBox (package:lessphone/app/tasks/tasks_repo.dart:15)
E/flutter ( 6440): <asynchronous suspension>
E/flutter ( 6440): #14     TasksRepo.getTasks (package:lessphone/app/tasks/tasks_repo.dart:23)
E/flutter ( 6440): <asynchronous suspension>
E/flutter ( 6440): #15     TasksRepo.addTask (package:lessphone/app/tasks/tasks_repo.dart:52)
E/flutter ( 6440): <asynchronous suspension>
E/flutter ( 6440): #16     _TasksScreenState.build.<anonymous closure>.<anonymous closure> (package:lessphone/app/ui/tasks_screen.dart:56)
E/flutter ( 6440): #17     _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:635)
E/flutter ( 6440): #18     _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:711)
E/flutter ( 6440): #19     GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182)
E/flutter ( 6440): #20     TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365)
E/flutter ( 6440): #21     TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:312)
E/flutter ( 6440): #22     GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156)
E/flutter ( 6440): #23     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222)
E/flutter ( 6440): #24     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198)
E/flutter ( 6440): #25     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156)
E/flutter ( 6440): #26     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102)
E/flutter ( 6440): #27     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86)
E/flutter ( 6440): #28     _rootRunUnary (dart:async/zone.dart:1136)
E/flutter ( 6440): #29     _CustomZone.runUnary (dart:async/zone.dart:1029)
E/flutter ( 6440): #30     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931)
E/flutter ( 6440): #31     _invoke1 (dart:ui/hooks.dart:250)
E/flutter ( 6440): #32     _dispatchPointerDataPacket (dart:ui/hooks.dart:159)
E/flutter ( 6440): 

@aswinmohanme
Copy link
Author

Here is my generated 'g.part'

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'task.dart';

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

class TaskAdapter extends TypeAdapter<Task> {
  Task read(BinaryReader reader) {
    var obj = Task();
    var numOfFields = reader.readByte();
    for (var i = 0; i < numOfFields; i++) {
      switch (reader.readByte()) {
        case 0:
          obj.title = reader.readString();
          break;
        case 1:
          obj.done = reader.readBool();
          break;
        case 2:
          obj.id = reader.readInt();
          break;
      }
    }
    return obj;
  }

  void write(BinaryWriter writer, Task obj) {
    writer.writeByte(3);
    writer.writeByte(0);
    writer.write(obj.title);
    writer.writeByte(1);
    writer.write(obj.done);
    writer.writeByte(2);
    writer.write(obj.id);
  }
}

@simc
Copy link
Member

simc commented Aug 2, 2019

Thanks, I'll investigate that.

@simc
Copy link
Member

simc commented Aug 4, 2019

I published a new version of hive_generator which should solve your problem. Please reopen the issue if it still doesn't work...

@simc simc closed this as completed Aug 4, 2019
@sh0umik
Copy link

sh0umik commented Sep 3, 2019

I am getting the same error

@simc
Copy link
Member

simc commented Sep 3, 2019

I am getting the same error

Please open another issue with details...

@shafi49
Copy link

shafi49 commented Jul 14, 2020

I am getting the same error

Please open another issue with details...

I was getting the same error while opening a box with 327 custom objects. Struggling for a while. Suddenly it disappeared with the same code. No clue what is happening here!

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

No branches or pull requests

4 participants