Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: The method 'listenable' isn't defined for the type 'Box' (using Hive)

Tags:

flutter

I'm trying to read the contents of a Box using Hive, following this SO comment, but I get the following error:

The method 'listenable' isn't defined for the type 'Box'

The code in question is:

FutureBuilder(
      future: Hive.openBox<Contact>('testBox'),
      builder: (context, snapshot) {
        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()),
                  );
                },
              );
            }
          },
        );
      },
    ),

pubspec.yaml:

hive: ^1.4.1+1
hive_flutter:
  git:
    url: git://github.com/hivedb/hive.git
    path: hive_flutter

What I am trying to do is list the content of the box when the screen loads. I can't seem to figure out where I am going wrong - any guidance would be greatly appreciated!

like image 898
Mr Jax Avatar asked Jul 03 '20 08:07

Mr Jax


1 Answers

The solution: hive_flutter.dart needs to be imported first

import 'package:hive_flutter/hive_flutter.dart';

like image 148
Mr Jax Avatar answered Oct 03 '22 23:10

Mr Jax