kike
kike

Reputation: 738

How to reset a Hive Database in Flutter

I am playing around with the Hive Database for Flutter, and when I am trying to save an object, the documentations says about Adapters clearly:

As you can see, each field has a unique number (unique per class). These field numbers are used to identify the fields in the Hive binary format, and should not be changed once your class is in use.

So, after some refactoring, I need to change the classes and some attributes, including his numeration. After doing that obviously the database gives me error (was working perfectly before), and given that all the data from the database is dum data for testing purposes, I would like to Reser and restart a new Database form 0. I did try several things, like box.clear() or Hive.deleteFromDisk() after opening the boxes, but I am still getting an error about the Adapter attribute type does not match with another in the normal class (referencing the one before refactoring and changing the index).

How can I successfully remove all information and clean the database to start from Zero as a new install??

Thanks a lot!

Upvotes: 9

Views: 7548

Answers (2)

kike
kike

Reputation: 738

Okkey, I found the answer:

The way to start the app as a new installation in the emulator is to uninstall it from the emulator and install it again.

Of course, if new changes to the index number in the Adapter, I would need to recreate these steps:

-Delete the adapters created

-Run flutter clean

-Delete the app from the emulator

-Create the new adapters with flutter packages pub run build_runner build

-Run the app again

Hope is useful for someone in the future :)

Upvotes: 14

Adam Musa
Adam Musa

Reputation: 1302

Ok, I see I had the same problem and I solved it in the following way:

  1. First of all, delete the .hive and .lock files generated by hive when you first launch your application in the parent directory of your project.

  2. Come back to modify it is what you want in your model and launch the following command:

flutter packages pub run build_runner build

Upvotes: 1

Related Questions