Hive: 160ms to 200ms to initialize and open a box.

Created on 17 Feb 2020  Â·  3Comments  Â·  Source: hivedb/hive

Question
I created a function which takes over 150ms and often 200ms to load:

    await Hive.initFlutter();
    final authBox = await Hive.openBox(STORED_AUTH_STATE);

Is this normal? The delay is noticeable because I call it before my runApp function as the app depends on local state.

The delay creates a black screen on the app before it starts (since it's running the call to hive).

Code sample

boostrap().then((widget) => runApp(widget));

Version
[✓] Flutter (Channel stable, v1.12.13+hotfix.8, on Mac OS X 10.14.6 18G103, locale en-US)
• Flutter version 1.12.13+hotfix.8 at /Users/anton/Personal/flutter
• Framework revision 0b8abb4724 (5 days ago), 2020-02-11 11:44:36 -0800
• Engine revision e1e6ced81d
• Dart version 2.7.0

question

Most helpful comment

@abacaj Using runApp multiple times is considered good practice:

main() async {
  runApp(Center(child: CircularProgressIndicator()));
  await Hive.initFlutter();
  final authBox = await Hive.openBox(STORED_AUTH_STATE);
  // ...
  runApp(MyApp());
}

All 3 comments

Looks like the delay originates from WidgetsFlutterBinding.ensureInitialized();

I wonder if there is a way around that. May need to show a temp loader while this call happens.

@abacaj Using runApp multiple times is considered good practice:

main() async {
  runApp(Center(child: CircularProgressIndicator()));
  await Hive.initFlutter();
  final authBox = await Hive.openBox(STORED_AUTH_STATE);
  // ...
  runApp(MyApp());
}

I wonder if there is a way around that. May need to show a temp loader while this call happens.

This call is only needed to make sure path_provider returns a valid path. If you can provide a path yourself, you can use Hive.init().

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ferdzzzzzzzz picture Ferdzzzzzzzz  Â·  3Comments

yannickvg picture yannickvg  Â·  4Comments

sergiyvergun picture sergiyvergun  Â·  4Comments

SergeShkurko picture SergeShkurko  Â·  4Comments

NourEldinShobier picture NourEldinShobier  Â·  3Comments