void deleteTask(int index) {
setState(() {
db.toDoList.removeAt(index);
});
db.updateDataBase();
}
this is what the delete function looks like how can i make the edit function for hive using flutter in that manner as above stated? please help
CodePudding user response:
Using the Hive database, there is a get(), getAt() put(), putAt(), delete(), deleteAt() methods that are well-documented from it's official documentation.
Hive is a key-value based database, there is no update() method by default, but you can achieve the same as only with the provided methods (getAt() and putAt()).
Considering that I have a "stringText" value stored on the 5 index, as we know to get it from a box, we can do the:
String valueFromTheBox = box.getAt(5); // "stringText"
And, in order to achieve and update this value, we simply need to assign a new value to that valueFromTheBox variable and put it again on the same key using putAt() like this:
valueFromTheBox = "newValueTHatWillBePut";
box.putAt(5);
This will literally make an update method, so in order to make a full function that achieves, and based on your case we can do:
void updateTask(int index) {
SetState(() {
dynamic task = db.toDoList.getAt(index); // get previous task
task = changeSomethingAndReturn(previousTask); // change/edit the task
db.toDoList.putAt(index, task); // assign the task on same index
});
db.updateDataBase();
}
And you need to replace changeSomethingAndReturn() method with your method that takes the task and makes changes over it then returns the new changed one.
Note: I don't recommend letting the dynamic type, since it's not mentioned in your question, I'm using it, but you should specify its type so you prevent getting into errors.
