Home > Net >  The same instance of an HiveObject cannot be stored in two different boxes in flutter
The same instance of an HiveObject cannot be stored in two different boxes in flutter

Time:12-04

I have created a simple learning app with hive..

here I want to store all deleted expense record into separate file,(box) so that I can get it restored on request..

I don't want to store deleted records in same file,

I checked history questions regarding this issue but not getting exact reason.

concept is simple to create a another file with same object type...in future I would go with backup and restore ....

here is my code

class Boxes
{
  static Box<ExpenseModel> getExpenseRecords()
  {
    return Hive.box<ExpenseModel>('expenserecords');
  }

  static Box<ExpenseModel> getDeletedRecords()
  {
    return Hive.box<ExpenseModel>('deletedrecords');
  }
}

main file

void main() async
{

  WidgetsFlutterBinding.ensureInitialized();
  var dir_path=await getApplicationDocumentsDirectory();
  Hive.init(dir_path.path);
  Hive.registerAdapter(ExpenseModelAdapter());
  Hive.registerAdapter(CategoryModelAdapter());

  await Hive.openBox<ExpenseModel>('expenserecords');
  await Hive.openBox<ExpenseModel>('deletedrecords');



  runApp(MyApp());
}

and delete function


Expanded(
              child: ValueListenableBuilder<Box<ExpenseModel>>(
                valueListenable:Boxes.getExpenseRecords().listenable(),

                builder: (context, box, _) {
                  //todo study which index is exactly taken
                  var data = box.values
                      .toList();
                  return ListView.builder(
                      itemCount: data.length,
                      itemBuilder: (context, index) {
                        final exp = data[index];
                        return ExpenseCard(
                          exp: exp,
                          ondelete: () {

                            final rcyclebox=Boxes.getDeletedRecords();
                            rcyclebox.add(exp);//

                            exp.delete();
                            //this delete method belongs to hiveobject
                          },
                          onedit: () {
                          },
                          ontoogle: (){
                            exp.isexpense=!exp.isexpense;
                            box.putAt(index, exp);
                          },

                        );
                      });
                },
              )

expense model classs

@HiveType(typeId: 1)
class ExpenseModel extends HiveObject
{
  @HiveField(0)
  String title;
  @HiveField(1)
  CategoryModel category;
  @HiveField(2)
  int amount;
  @HiveField(3)
  bool isexpense;
  ExpenseModel({required this.title,required this.category,required this.amount,required this.isexpense});

}


CodePudding user response:

The problem is that you are getting objects from Hive and saving the same object instance (after modification of course, but the object reference is the same). Hive knows this is the same object and complains that I can't store the same thing again. So you will have to create a new object instance. You can create a copywith method that returns a new object.

  • Related