4

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

Essentially I have these in my dependencies so everything should be good.

  hive: ^1.4.4+1
  hive_flutter: ^0.3.1
  path_provider: ^1.6.27

I also have import 'package:hive/hive.dart'; and import 'package:path_provider/path_provider.dart'; in the file

So I just have

void doSomething() async {
    final documentDirectory = await getApplicationDocumentsDirectory();
    Hive.init(documentDirectory.path);
  }

called.

I do not understand. I think I've done everything correct. Let me know if you need something else.

mck
  • 40,932
  • 13
  • 35
  • 50

8 Answers8

6

Hive needs to be initialized when it runs on Android or iOS, therefore you can use a function like this:

Future<Box> openHiveBox(String boxName) async {
    if (!kIsWeb && !Hive.isBoxOpen(boxName)) 
      Hive.init((await getApplicationDocumentsDirectory()).path);
    
    return await Hive.openBox(boxName);
}

You'll need to import path_provider in order to access getApplicationDocumentsDirectory()

Leonardo Rignanese
  • 865
  • 11
  • 22
5

Try the following code on the main function of your flutter application:

import 'package:path_provider/path_provider.dart';
import 'package:hive/hive.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final appDocumentDirectory = await getApplicationDocumentsDirectory();
  Hive.init(appDocumentDirectory.path);
}
Pratik Adhikari
  • 427
  • 5
  • 12
1

I guess you are getting this issue because you are not awaiting the initFlutter.

import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:path_provider/path_provider.dart' as path_provider;


Future<void> yourFunction() async {
  final dbDir = await path_provider.getApplicationDocumentsDirectory();

  // init hive
  await Hive.initFlutter(dbDir.path);
  await openYourBox();
}
Nirjan Munshi
  • 166
  • 2
  • 11
1

I think you should await your init method.

Saad Lembarki
  • 482
  • 5
  • 6
0

Currently, path_provider doesn't support WEB. You can see it here: path_provider.

You have to use another directory for WEB. If you are using BLOC as a state management, you could do something like this:

if (!kIsWeb) {
    // if android or tablet
    HydratedBloc.storage = await HydratedStorage.build(
        storageDirectory: await getApplicationDocumentsDirectory(),
    );
} else {
    // if web
    HydratedBloc.storage = await HydratedStorage.build(
        storageDirectory: HydratedStorage.webStorageDirectory,
    );
}
S. M. JAHANGIR
  • 4,324
  • 1
  • 10
  • 30
0

I got this error because of a typo:

await Hive.initFlutter;

should've been

await Hive.initFlutter();
buckleyJohnson
  • 459
  • 4
  • 12
0

Just follow these steps the problem will be solved. Import these two packages

import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';

and then use hive

await Hive.initFlutter();
await Hive.openBox('cache');
Sammrafi
  • 399
  • 4
  • 8
-1

Actually you don't need use HydratedStorage to initialize Hive on web:

import 'package:hive/src/hive_impl.dart';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';

initializeHive()async{
  //Use HiveImpl() to ensure you don't have conflicting Hive boxes.
  HiveInterface _hive = HiveImpl();
  if (kIsWeb) {
    await _hive.openBox('yourBoxName');
  } else {
    var dir = await getApplicationDocumentsDirectory();
    _hive.init(dir.path);
    await _hive.openBox('yourBoxName');
  }
}

If you're using Flutter on web, you don't need to initialize Hive and neither provider a path to box, only if you're using it on mobile.