Updating information within a list of objects with Flutter Hive

I am working with Hive for the first time to save data within flutter and am halfway there...

I have a list of weeks that I am saving within a box, and I am able to save those weeks successfully with:

  void addWeek({
    required double budget,
  }) {
    Week newWeek = Week(
      budget: budget,
    );

    listOfWeeks.add(newWeek);
    box.add(newWeek);
    notifyListeners();
  }

When I close the app and restart it I then can load all those saved weeks within the class constructor with:

@HiveType(typeId: 1)
class WeekList extends ChangeNotifier {
  WeekList() {
    int boxLength = box.length;
    for (var i = 0; i < boxLength; i++) {
      listOfWeeks.add(box.get(i));
    }
  }

What I am struggling with wrapping my head around is how to access the data and update the data within my list of weeks. The listOfWeeks holds a bunch of objects type Week which looks like this:

@HiveType(typeId: 2)
class Week extends ChangeNotifier {
  // Constructor
  Week({
    this.budget = 0.0,
  });

  //budget
  @HiveField(0)
  double budget = 0.0;
  @HiveField(1)
  String dailySpend = '\$71.43 per day';

  @HiveField(2)
  double totalSpent = 0.0;
  @HiveField(3)
  double percentLeftInBudget = 0.0;

  //Spent days of the week
  @HiveField(4)
  double sunday = 0.0;
  @HiveField(5)
  double monday = 0.0;
  @HiveField(6)
  double tuesday = 0.0;
  @HiveField(7)
  double wednesday = 0.0;
  @HiveField(8)
  double thursday = 0.0;
  @HiveField(9)
  double friday = 0.0;
  @HiveField(10)
  double saturday = 0.0;

I figure in all the update functions for all the values I need to use a box.put() but I can't seem to wrap my head around the syntax for this. WOuld it be something like:

  //updateBudget
  void updateBudget({required double newBudget, required int index}) {
    listOfWeeks[index].budget = newBudget;
    box.put(index,listOfWeeks[index].budget);
    notifyListeners();
  }

but then if I moved to the next update function for updating let's say the value of saturday I just cant use box.put(index,listOfWeeks[index].saturday);

I tried box.put(listOfWeeks[index]).budget to access the specific value but that doesn't seem to work. Any help would be greatly appreciated, thanks!



Comments

Popular posts from this blog

Spring Elasticsearch Operations

Today Walkin 14th-Sept

Object oriented programming concepts (OOPs)