0

From my Hive box I have created a list of Widgets of a certain specified type.

  Hive.box<Job>('jobBox').values.where((element) => element.jobType == selectedType).toList().map(
        (jobList)

I have create a favorites button which generates from the initial stored values in the Hive box, so if it is already set to true or false.

              GestureDetector(
                  child: Icon(
                    jobList.fav
                        ? Icons.favorite
                        : Icons.favorite_border,
                    color: jobList.fav ? Colors.red.shade200 : Colors.grey,
                  ),
                  onTap: () {
                    if (jobList.fav == true){
                      setState(() {
                        jobList.fav = false;
                       // Hive.box<Job>('jobBox').putAt(); //This is where I am having my problem
                        print(jobList.jobID );
                        print(jobList.fav );
                      });

                    }
                    else{
                      setState(() {
                        jobList.fav = true;
                        // Hive.box<Job>('jobBox').putAt(); //This is where I am having my problem
                        print(jobList.jobID );
                        print(jobList.fav );
                      });
                    }
                  }
                // )
              ),

The setState updates the favorite icon and changes the value, but on closing and reopening the app it restores the original default values in the Hive box. Which makes sense as I haven't updated the Hive box.

But what is the correct way to update this fav value in the Hive box? I have tried putAt using the jobID for the index, which I assume gets me to the correct record. But then how to I just change the fav field from true to false or from false to true?

Do I have to overwrite all the field values in the box at this index?

Hive.box<Job>('jobBox').putAt(jobList.jobID, ???);
mck
  • 40,932
  • 13
  • 35
  • 50
rjh500
  • 45
  • 9

1 Answers1

1

So I changed this

jobList.fav = true;
// Hive.box<Job>('jobBox').putAt();

To this:

 jobList.fav = true;
 Hive.box<Job>('jobBox').putAt(jobList.jobID-1, job);

I had to do -1 as my job ID starts at 1, and the index on the Hive box starts at 0. I can go back and change this later. I did not find a way to just update the fav value in the Hive Box, but if I do it this way, by updating the list then putting that item into the box at the same index it did persist when closing the app and reopening the app.

But then once it lost the connection to the IDE terminal it did not persist on future changes and app restarts. Is there a reason for this?

rjh500
  • 45
  • 9
  • After wiping the emulator and starting with a fresh box the data seems to be persisting correctly now on multiple changes and restarts. – rjh500 May 12 '21 at 17:37