flutterdartdart-pubservice-locatorget-it

Hive adapters and boxes with Get_it how should i regester them into getit


I have always issues with hive boxies instances and the error of not initilizing the box or adapter so I decided to register them in one place and use it all over place .but I don't event know how to use and register adapters and box

I have nothing tried because I don't know how to implement that


Solution

  • The recommended place to register an adapter using Hive.registerAdapter() is immediately after the Hive.initFlutter() finishes, then open your box with the object type that you made the adapter for, like this:

    Future<void> main() async {
      await Hive.initFlutter();
      Hive.registerAdapter<Question>(QuestionAdapter());
      await Hive.openBox<Question>("questions");
    
      runApp(MyApp());
    }
    

    now the "questions" box will be available to use directly across your whole app by :

    Hive.box("questions");