Sumchans
Sumchans

Reputation: 3774

flutter opening a Hive box gives error -says box not found, Did you forget to call Hive openbox()

I have a class where I am doing the graphql setup and the hive box setup. Here is the class -

class GraphQLConfiguration {
  ValueNotifier<GraphQLClient> client = new ValueNotifier<GraphQLClient>(
    GraphQLClient(
      cache:
      GraphQLCache(store: HiveStore(Hive.box(HiveStore.defaultBoxName))),
      link: HttpLink('http://localhost:4000/graphql/',),
    ),
  );
  GraphQLConfiguration()  {
    initializeHive();
  }
  void initializeHive() async {
    await initHiveForFlutter(); // or await initHiveForFlutter();
    await Hive.openBox('bolBox');
  }
}

Now I initialize this class in the Flutter main method -

Future main() async {
   GraphQLConfiguration graphql = new GraphQLConfiguration();
}

When I run this code I get this error message -

Error - Unhandled Exception: HiveError: Box not found. Did you forget to call Hive.openBox()?

I followed this post as well Box not found. Did you forget to call Hive.openBox()?, didn't help.

Upvotes: 0

Views: 1583

Answers (2)

Vamsi Krishna
Vamsi Krishna

Reputation: 363

Add initHiveForFlutter in your root folder & it solve the problem.

void main() async{
  await initHiveForFlutter();
  runApp(MyApp());
}

Worked for me.

No need to initialise with open box & path as GraphQl handles that internally inside initHiveForFlutter.

Upvotes: 0

Hemal Moradiya
Hemal Moradiya

Reputation: 2097

Initialize Hive by giving it a home directory by using path_provider

final Directory appDocDir = await getApplicationDocumentsDirectory();
Hive.init(appDocDir.path);

then open box

await Hive.openBox('bolBox');

Upvotes: 1

Related Questions