【问题标题】:flutter opening a Hive box gives error -says box not found, Did you forget to call Hive openbox()颤振打开 Hive 框会给出错误 - 说找不到框,您是否忘记调用 Hive openbox()
【发布时间】:2021-09-02 18:28:45
【问题描述】:

我有一堂课,我正在做 graphql 设置和蜂巢箱设置。这是课程-

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');
  }
}

现在我在 Flutter 的 main 方法中初始化这个类 -

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

当我运行此代码时,我收到此错误消息 -

错误 - 未处理的异常:HiveError:找不到框。您是否忘记调用 Hive.openBox()?

我也关注了这个帖子Box not found. Did you forget to call Hive.openBox()?,没有帮助。

【问题讨论】:

    标签: flutter flutter-hive


    【解决方案1】:

    通过使用 path_provider 为其提供主目录来初始化 Hive

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

    然后打开盒子

    await Hive.openBox('bolBox');
    

    【讨论】:

      【解决方案2】:

      在您的根文件夹中添加 initHiveForFlutter 即可解决问题。

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

      为我工作。

      无需使用打开的框和路径进行初始化,因为 GraphQl 在 initHiveForFlutter 内部处理。

      【讨论】:

        猜你喜欢
        • 2015-09-05
        • 1970-01-01
        • 2021-07-29
        • 2020-11-28
        • 2020-03-20
        • 2022-11-28
        • 2021-11-12
        • 2018-11-21
        • 2021-07-04
        相关资源
        最近更新 更多