将Hive与扑朔迷离的本地数据副本集成

发布于 2025-01-23 10:39:57 字数 3356 浏览 0 评论 0原文

我正在尝试使用Resocoder提出的代码将蜂巢与颤动集成在一起。一切都很好,但是我被困在一个我无法弄清楚该怎么办的地方。如果您看到Contactsbox提出错误,说明Itsa n对象,我不能在其上使用Lingth属性,甚至无法使用任何其他属性。我以粗体投掷错误向代码的部分推销。任何想法为什么会发生这个错误。我应该如何使用Contactsbox作为无法理解的列表。任何帮助都是很好的

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文