Integrating Hive with Flutter for local data copy

Viewed 63

I am trying the code put by Resocoder for integrating the Hive with Flutter. Everything was going fine but i got stuck at a place from where i cant figure out what to do. If you see contactsbox is throwing error stating that itsa n object and i cant use lingth property on it or even any other property. I have market the partof the code in bold throwing error. Any idea why is this error happening. HOw shall i use contactsBox as List unable to understand it. Any help would be great

contact_page.dart

import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'new_contact_form.dart';
import 'contact.dart';

class ContactPage extends StatelessWidget {
  const ContactPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Hive Tutorial'),
      ),
      body: Column(
        children: [
          Expanded(
            child: _buildListView(),
          ),
          NewContactForm(),
        ],
      ),
    );
  }

  Widget _buildListView() {
    return ValueListenableBuilder(
        valueListenable: Hive.box('contacts').listenable(), builder:
        (context, contactsBox, _) {
      return ListView.builder(**itemCount: contactsBox.length** , itemBuilder: (context, index) {
        final contact = contactsBox.getAt(index) as Contact;

        return ListTile(
          title: Text(contact.name!),
          subtitle: Text(contact.age.toString()),
          trailing: Row(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              IconButton(
                icon: Icon(Icons.refresh),
                onPressed: () {
                  contactsBox!.putAt(
                    index,
                    Contact('${contact.name}*', contact.age + 1),
                  );
                },
              ),
              IconButton(
                icon: Icon(Icons.delete),
                onPressed: () {
                  contactsBox.deleteAt(index);
                },
              )
            ],
          ),
        );
      }
      );
    }
    );
  }

new_contact_form.dart

import 'package:db_app/contact.dart';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';

class NewContactForm extends StatefulWidget {
  const NewContactForm({Key? key}) : super(key: key);

  @override
  State<NewContactForm> createState() => _NewContactFormState();
}

class _NewContactFormState extends State<NewContactForm> {
  final _formKey = GlobalKey<FormState>();
  String? _name;
  String? _age;

  void addContact(Contact cnt) {
    final contactBox = Hive.box('contacts');
    contactBox.add(cnt);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Form(
      child: Column(
        children: [
          TextFormField(
            decoration: InputDecoration(labelText: 'Name'),
            onSaved: (value) => _name = value,
          ),
          SizedBox(height: 10),
          TextFormField(
            decoration: InputDecoration(labelText: 'Age'),
            onSaved: (value) => _age = value,
            keyboardType: TextInputType.number,
          ),
          ElevatedButton(
            onPressed: () {
              _formKey.currentState!.save();
              final newContact = Contact(_name, int.parse(_age!));
              addContact(newContact);
            },
            child: Text('Add New Contact'),
          ),
        ],
      ),
    ));
  }
}
0 Answers
Related