Flutter Hive Update

34 Views Asked by At
return ListTile(
      tileColor: componnentsColor,
      leading: IconButton(
        icon: widget.isDone? const Icon(Icons.check_box) : const Icon(Icons.check_box_outline_blank),
        color: textColor,
        onPressed: () {
          setState(() {
            widget.isDone = !widget.isDone;
            if (widget.isList){
              Lists dbList= listsBox.get(widget.listName);
              dbList.tasks.add(Task(isDone: widget.isDone, task: widget.taskName, description: widget.taskDescription, date: widget.taskDate));
              listsBox.put(widget.listName, Lists(isDone: dbList.isDone, task: dbList.task, description: dbList.description, tasks: dbList.tasks));
            }else{
              taskBox.put(widget.taskName, Task(isDone: widget.isDone, task: widget.taskName, description: widget.taskDescription, date: widget.taskDate));
            }
          });
        },
      ),
      title: Text(
        widget.taskName,
        style: const TextStyle(
          color: textColor,
        ),
      ),
      subtitle: Text(
        '${widget.taskDate}',
        style: const TextStyle(
          color: textColor,
        ),
      ),
      trailing: const Icon(
        Icons.expand_more,
        color: textColor,
      ),
      onTap: () {
      },
    );

this code and exactly when i change the state. i update the local variable: widget.isDone = !widget.isDone; i check if it's a list or not via the variable isList; and i want to update my hive database using this code : if (widget.isList){ Lists dbList= listsBox.get(widget.listName); dbList.tasks.add(Task(isDone: widget.isDone, task: widget.taskName, description: widget.taskDescription, date: widget.taskDate)); listsBox.put(widget.listName, Lists(isDone: dbList.isDone, task: dbList.task, description: dbList.description, tasks: dbList.tasks)); } the problem:

it duplicates the todo in my list so instead of one with the updated isDone variable i get two: one updated and one is not updated..... any idea what's going on?

1

There are 1 best solutions below

0
Majdouli Oussama On

i found the problem. i was duplicating it myself by using list.add...

Instead I needed to use: dbList.tasks[dbList.tasks.indexWhere((element) => element?.task == widget.taskName)]?.isDone = widget.isDone;