I have a list of objects called Weeks in my project and after I run the delete function to remove items from my Hive box the code runs fine. But then when I restart the app and it initializes and loads items from the box I get this error:
_TypeError (type 'Null' is not a subtype of type 'Week')
My delete function looks like this:
//Delete Week
void deleteWeek({required int weekIndex}) {
print(box.length);
listOfWeeks.removeAt(weekIndex);
box.deleteAt(weekIndex);
print(box.length);
notifyListeners();
}
I thought at first maybe I was deleting too much from box, so I print out the length and the length goes down by one after it runs box.deleteAt(weekIndex); in the above function.
My add week function works perfect and has no issues after restarting the app:
void addWeek({
required double budget,
}) {
Week newWeek = Week(
budget: budget,
);
listOfWeeks.add(newWeek);
box.add(newWeek);
notifyListeners();
}
Since the delete function is basically the add function in reverse I am not sure what I am doing wrong. My only hypothesis at this point is that the box.deleteAt() function is not operating how I expect it too. Does it not work similar to the listOfWeeks.removeAt(weekIndex); ?
For more info, the weeks class initializes with this at restart:
@HiveType(typeId: 1)
class WeekList extends ChangeNotifier {
WeekList() {
print("Loading box");
int boxLength = box.length;
for (var i = 0; i < boxLength; i++) {
listOfWeeks.add(box.get(i));
}
}