ensureInitialized static method

Future<Box?> ensureInitialized()

Opens (or reuses) the configured Hive box.

Safe to call multiple times and from multiple screens: concurrent calls share the same opening future. If the host already opened the box or initialized Hive, its configuration is left untouched.

Implementation

static Future<Box<dynamic>?> ensureInitialized() {
  final Box<dynamic>? box = _box;
  if (box != null && box.isOpen) return Future<Box<dynamic>?>.value(box);
  return _opening ??= _open().whenComplete(() {
    _opening = null;
  });
}