Thanoss
Thanoss

Reputation: 533

Using Hive.box("user") throws the error The box "user" is already open and of type Box<User>

I know this question has been asked before, but the solution doesn't help me. according to the hive doc, an opened box can be called like this Hive.box('myBox'); when I try this in my code I get the error

The box "user" is already open and of type Box<User>.

anyone has an idea why this happens.

Upvotes: 0

Views: 818

Answers (3)

gilbriatore
gilbriatore

Reputation: 678

I have the same problem and the solution proposed by Ray Zion works perfectly for me.

If you opened a hive box using generic await Hive.openBox<String>('user_api'), you need to use it later using generic Hive.box<String>('user_api').

Upvotes: 3

Enis Erkaya
Enis Erkaya

Reputation: 81

i faced the same problem after migrate flutter null safety. Set box object as non nullable. Error is disappear. Wrong code:

late Box<User?> userBox;
Hive.box<User?>(HiveBoxes.userBox);

Correct:

Box<User> userBox = Hive.box<User>(HiveBoxes.userBox);

Upvotes: 0

Madhavam Shahi
Madhavam Shahi

Reputation: 1192

Try this

void stuff() async {
  //Hive.init('somePath') 

  var box = await Hive.openBox('user');

  box.put('name', 'David');
  
  print('Name: ${box.get('name')}');
}

Upvotes: 0

Related Questions