felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.72k stars 3.38k forks source link

fix: Adding a new hydrated cubit to an app that already has state causes errors from Hive. #3243

Closed zmoshansky closed 2 years ago

zmoshansky commented 2 years ago

Description I've added a new hydratedCubit to an app (Android), that has already persisted data, and upon calling HydratedStorage.build it errors. The cubit I added has a single required field String id, and is built with freezed (AFAICT, shouldn't matter). When running the app inside a fresh web-browser profile (storage is clear) everything works fine.

I'm unsure if this is a Hive issue, or an issue with HydratedBloc's use of Hive.

E[/flutter]() (21463): [ERROR:flutter[/lib/ui/ui_dart_state.cc]()(209)] Unhandled Exception: HiveError: This should not happen. Please open an issue on GitHub.
E[/flutter]() (21463): #0      BinaryReaderImpl.readFrame
package:hive/…/binary/binary_reader_impl.dart:250
E[/flutter]() (21463): #1      FrameHelper.framesFromBytes
package:hive/…/binary/frame_helper.dart:21
E[/flutter]() (21463): #2      FrameIoHelper.framesFromFile
package:hive/…/io/frame_io_helper.dart:42
E[/flutter]() (21463): <asynchronous suspension>
E[/flutter]() (21463): #3      StorageBackendVm.initialize
package:hive/…/vm/storage_backend_vm.dart:86
E[/flutter]() (21463): <asynchronous suspension>
E[/flutter]() (21463): #4      HiveImpl._openBox
package:hive/src/hive_impl.dart:111
E[/flutter]() (21463): <asynchronous suspension>
E[/flutter]() (21463): #5      HiveImpl.openBox
package:hive/src/hive_impl.dart:139
E[/flutter]() (21463): <asynchronous suspension>
E[/flutter]() (21463): #6      HydratedStorage.build.<anonymous closure>
package:hydrated_bloc/src/hydrated_storage.dart:101
E[/flutter]() (21463): <asynchronous suspension>
E[/flutter]() (21463): #7      BasicLock.synchronized
package:synchronized/src/basic_lock.dart:33
E[/flutter]() (21463): <asynchronous suspension>
E[/flutter]() (21463): #8      main
package:foo/main.dart:24
E[/flutter]() (21463): <asynchronous suspension>

Steps To Reproduce 1.) Create an App with a hydratedCubit 2.) Launch app and have cubit save state 3.) Add a new hydratedCubit to the app 4.) Calling HydratedStorage.build it errors from Hive.

Expected Behavior This should just work, or cause some error in my deserialization code.

Additional Context When running the app inside a fresh web-browser profile (storage is clear) everything works fine. This also relates to https://github.com/felangel/bloc/issues/2613 w

zmoshansky commented 2 years ago

After clearing app storage, everything works properly on Android... However, this is obviously not a workable solution for a production app.

felangel commented 2 years ago

Hi @zmoshansky 👋 Thanks for opening an issue!

Are you able to provide a link to a minimal reproduction sample? Thanks!

zmoshansky commented 2 years ago

I'm going to close this as I strongly suspect this underlying issue is the root cause of the issue (Many reported bugs around corruption).

https://github.com/hivedb/hive/issues/263