当我尝试在Flutter中访问Hive数据库时出现此错误。已经打开并且类型为Box< Contact> [英] i got this error when i tried to access Hive database in flutter The box "contacts" is already open and of type Box<Contact>

查看:120
本文介绍了当我尝试在Flutter中访问Hive数据库时出现此错误。已经打开并且类型为Box< Contact>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在main中初始化了Box数据库,如下所示

I initialized box database in main as follow

    void main() async {

  WidgetsFlutterBinding.ensureInitialized();
  final appDocumentDirectory = await path_provider.getApplicationDocumentsDirectory();
  Hive.init(appDocumentDirectory.path);
  Hive.registerAdapter(ContactAdapter());
  runApp(MyApp());
}

然后我使用FutureBuilder插件在材质应用中打开框,如下所示

then I open box in the material app by using FutureBuilder plugin as follow

FutureBuilder(
    future: Hive.openBox<Contact>('contacts'),
    builder: (context, snapshot) {
      if(snapshot.connectionState == ConnectionState.done){
        if(snapshot.hasError){
          return Text(snapshot.error.toString() );
        }
        return ContactPage();
      } else {
        return Scaffold();
      }
    }
  ),

并在ContactPage()内

and inside ContactPage()

我创建了这个:-

ValueListenableBuilder(
              valueListenable: Hive.box<Contact>('contacts').listenable(),
              builder: (context,Box<Contact> box,_){
                if(box.values.isEmpty){
                  return Text('data is empty');
                } else {
                  return ListView.builder(
                    itemCount: box.values.length,
                    itemBuilder: (context,index){
                      var contact = box.getAt(index);
                      return ListTile(
                        title: Text(contact.name),
                        subtitle: Text(contact.age.toString()),
                      );
                    },
                  );
                }
              },

            )

我运行应用程序时遇到以下错误

when I run the application I get the following error

    The following HiveError was thrown while handling a gesture:
The box "contacts" is already open and of type Box<Contact>.

当我尝试使用未打开的盒子时,出现错误,表示盒子未打开

and when I tried to use the box without opening it, i got error mean the box is not open

我是否必须使用框而不在ValueListenableBuilder中打开框?
,但随后我必须在不同的小部件中再次打开同一框来添加数据

do I have to use box without opening it inside ValueListenableBuilder ? but then i have to open same box again in the different widget to add data on it

推荐答案

我之所以跳上这个线程,是因为我在尝试使用resocoder的Hive教程时很难弄清楚如何处理过时的WatchBoxBuilder,而Google搜索将我引到了这里。

I'm jumping on this thread because I had a hard time trying to figure out how to deal with the deprecated WatchBoxBuilder while using the resocoder's Hive tutorial, and a google search led me here.

这就是我最终使用的内容:

This is what I ended up using:

main.dart:

main.dart:

void main() async {
  if (!kIsWeb) { // <-- I put this here so that I could use Hive in Flutter Web
    final dynamic appDocumentDirectory =
        await path_provider.getApplicationDocumentsDirectory();
    Hive.init(appDocumentDirectory.path as String);
  }
  Hive.registerAdapter(ContactAdapter());

  runApp(child: MyApp());
}

然后是ContactPage()(注意:与OP相同):

and then ContactPage() (note: it's the same as OP's):

Widget _buildListView() {
  return ValueListenableBuilder(
    valueListenable: Hive.box<Contact>('contacts').listenable(),
    builder: (context, Box<Contact> box, _) {
      if (box.values.isEmpty) {
        return Text('data is empty');
      } else {
        return ListView.builder(
          itemCount: box.values.length,
          itemBuilder: (context, index) {
            var contact = box.getAt(index);
            return ListTile(
              title: Text(contact.name),
              subtitle: Text(contact.age.toString()),
            );
          },
        );
      }
    },
  );
}

这篇关于当我尝试在Flutter中访问Hive数据库时出现此错误。已经打开并且类型为Box&lt; Contact&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆