0

I am trying to use Hive database in my flutter app. I have followed the youtube tutorials but the code is not working in my app. I am getting the following error.

Unhandled Exception: HiveError: You need to initialize Hive or provide a path to store the box.

Here is my code...

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final dbDir = await getApplicationDocumentsDirectory();
  await Hive.initFlutter(dbDir.path);
  Hive.registerAdapter(AddToCartAdapter());
  await Hive.openBox<AddToCart>('addToCart');
  runApp(const MyApp());
}
Future cartlist(String name, String unit, String qty, String price) async {
    await Hive.openBox("addToCart");
    final cart = AddToCart()
      ..name = name
      ..unit = unit
      ..qty = qty
      ..price = price;

    final Box = Boxes.getAddToCart();
    Box.add(cart)
;
  }
padaleiana
  • 955
  • 1
  • 14
  • 23
swapnil mane
  • 215
  • 5
  • 17

1 Answers1

1

I also use Hive as a local database. I noticed that I din't specify subDir param and just initiated like that:

await Hive.initFlutter();
Hive.registerAdapter(AddToCartAdapter());

The problem is occurring to you, because you are using Hive.initFlutter with dbDir.path path and while using Hive.openBox you are not specifying that path again, You need to specify same path for openBox method.

I suggest your code should be like that:

Future<void> main() async {
   WidgetsFlutterBinding.ensureInitialized();
   await Hive.initFlutter();
   Hive.registerAdapter(AddToCartAdapter());
   HiveBoxes.cartBox = await Hive.openBox<AddToCart>(Constants.cartBoxName);
   runApp(const MyApp());
 }

To make your code better, I suggest some stuffs like creating a class for constant names to prevent typos and creating some static variable to get access Hive Boxes easily like that:

class Constants{
    static const cartBoxName = 'CartBox';
}

class HiveBoxes{
   static late Box cartBox;
}

About your method you can do like that:

Future cartlist(String name, String unit, String qty, String price) async {
///we don't need to open box again, so please delete this commented code
//await Hive.openBox("addToCart");
final cart = AddToCart()
  ..name = name
  ..unit = unit
  ..qty = qty
  ..price = price;

HiveBoxes.cartBox.put("sampleKey", cart);;
// It's also wrong code: Box.add(cart);
}
Jakhongir Anasov
  • 1,207
  • 7
  • 16
  • `Unhandled Exception: HiveError: You need to initialize Hive or provide a path to store the box.` getting error – swapnil mane Dec 02 '22 at 10:13
  • I have updated my question sir... I really did not understand where to put that *same path for openBox* method. – swapnil mane Dec 02 '22 at 10:18
  • please update your answer with my new code sir. – swapnil mane Dec 02 '22 at 10:24
  • Please, pay attention that HiveBoxes and Constants are your custom classes not library related. – Jakhongir Anasov Dec 02 '22 at 10:41
  • Yes sir. now getting this error. `[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: LateInitializationError: Field 'cartBox' has not been initialized.` – swapnil mane Dec 02 '22 at 10:54
  • That's because you are interacting this box before initializeng it. Are you sure that you are executing HiveBoxes.cartBox = await Hive.openBox(Constants.cartBoxName); in main function. Did you restarted flutter application. – Jakhongir Anasov Dec 02 '22 at 11:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/250108/discussion-between-jakhongir-anasov-and-swapnil-mane). – Jakhongir Anasov Dec 03 '22 at 06:50
  • @swapnilmane could you fix your issue. If yes, please kindly accept the answer. If not, let me know what problem are you facing now? – Jakhongir Anasov Dec 03 '22 at 06:51
  • now getting new error at startup...Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null) – swapnil mane Dec 05 '22 at 06:54
  • I have questioned here...https://stackoverflow.com/questions/74684235/flutter-unhandled-exception-platformexceptionchannel-error-unable-to-establ – swapnil mane Dec 05 '22 at 06:58
  • Why you created another question? It doesn't seem like common thing in stackoverflow community. I mean if the issue is resolved, you need to accept the answer. The error you are getting know may not be related at all with this question. – Jakhongir Anasov Dec 05 '22 at 08:34
  • I accepted the answer,sir. but this time getting new error... – swapnil mane Dec 05 '22 at 08:36
  • I saw the new question. I wondered why you are still using "final document = await getApplicationDocumentsDirectory();" I even sent you the code sample, which doesn't require specifying doucment. – Jakhongir Anasov Dec 05 '22 at 08:37
  • I was using your code. but getting that platformexception error. SO i thought changing String to final may help to fix it but not worked. so, – swapnil mane Dec 05 '22 at 08:38
  • If you want we can try to fix your issue with Telegram, Zoom or etc – Jakhongir Anasov Dec 05 '22 at 08:41
  • Because it is getting big time to reach out as you are getting different errors and trying different things. – Jakhongir Anasov Dec 05 '22 at 08:41
  • Sorry sir. I am not able to use zoom or telegram. Can't we fix it here sir ?? – swapnil mane Dec 05 '22 at 08:45
  • I just wanted to help with live coding, as it would take much less time. By the way I answered your new question. Let's continue there – Jakhongir Anasov Dec 05 '22 at 08:47