0

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.

mck
  • 40,932
  • 13
  • 35
  • 50
Thanoss
  • 439
  • 5
  • 11

3 Answers3

2

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').

gilbriatore
  • 658
  • 7
  • 12
0

Try this

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

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

  box.put('name', 'David');
  
  print('Name: ${box.get('name')}');
}
Madhavam Shahi
  • 1,166
  • 6
  • 17
0

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);
Enis Erkaya
  • 71
  • 2
  • 6