0

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.

leftjoin
  • 36,950
  • 8
  • 57
  • 116
Sumchans
  • 3,088
  • 6
  • 32
  • 59

2 Answers2

1

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');
Hemal Moradiya
  • 1,715
  • 1
  • 6
  • 26
0

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.

Vamsi Krishna
  • 353
  • 3
  • 12