initHiveForFlutter function

Future<void> initHiveForFlutter({
  1. String? subDir,
  2. Iterable<String> boxes = const [HiveStore.defaultBoxName],
})

Initializes Hive with the path from getApplicationDocumentsDirectory.

You can provide a subDir where the boxes should be stored.

Extracted from hive_flutter source

Implementation

Future<void> initHiveForFlutter(
    {String? subDir,
    Iterable<String> boxes = const [HiveStore.defaultBoxName]}) async {
  WidgetsFlutterBinding.ensureInitialized();
  if (!kIsWeb) {
    var appDir = await getApplicationDocumentsDirectory();
    var path = appDir.path;
    if (subDir != null) {
      path = join(path, subDir);
    }
    HiveStore.init(onPath: path);
  }

  final futures = boxes.map((String name) => HiveStore.open(boxName: name));
  await Future.wait(futures);
}