框“联系人"将在列表中显示.已经打开并且类型为Box< Contact>试图在扑扑中访问Hive数据库时 [英] The box "contacts" is already open and of type Box<Contact> when trying to access Hive database in flutter

查看:84
本文介绍了框“联系人"将在列表中显示.已经打开并且类型为Box< Contact>试图在扑扑中访问Hive数据库时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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 follows:

  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.

我是否必须使用Box而不在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,谷歌搜索把我引到了这里.

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.

这是我最终使用的:

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()),
            );
          },
        );
      }
    },
  );
}

这篇关于框“联系人"将在列表中显示.已经打开并且类型为Box&lt; Contact&gt;试图在扑扑中访问Hive数据库时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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