isar / hive

Lightweight and blazing fast key-value database written in pure Dart.
Apache License 2.0
4.06k stars 404 forks source link

How to Change Value inside the box, when I am using the Type-Adapter and Model Class? #689

Closed shaadart closed 3 years ago

shaadart commented 3 years ago

Question I have come Across an Issue, I have a Model, Which Looks Like this.

class StreakModel {

  String streakName;

  int streakCount;

  String streakEmoji;

  String streakRemainder;

  int streakDays;

I have then written Some Values in them and used, streakBox.add(streak) As we all do, But the Question is, I want to Update up the int streakCount on the Button Press, by +1

I had Tried, streak.streakCount++; and every other tactic, but the Changes are not Stored Locally Effectively. Can I get the relevant answer to this? I am Hungry to fetch this.

Version

themisir commented 3 years ago

You could use HiveObject to quickly fix this issue. Firstly extend your model from HiveObject. Then call .save() after modifying model.

class StreakModel extends HiveObject {

  String streakName;

  int streakCount;

  String streakEmoji;

  String streakRemainder;

  int streakDays;
}
shaadart commented 3 years ago

@TheMisir I always look forward for your answers, they are really beautiful. Can I Get the minimal Code Example. I did not Got the Thing that you are needing to convey, I know it is beautiful. 🐝 #Hive

themisir commented 3 years ago

@madd-project check this example out:

class StreakModel extends HiveObject {

  String streakName;

  int streakCount;

  String streakEmoji;

  String streakRemainder;

  int streakDays;
}

void main() {
  final streak = Hive.box<StreakModel>('streaks').get(1); // assuming streaks box is already opened using (Hive.openBox)

  streak.streakDays++;
  streak.save();
}
shaadart commented 3 years ago

@TheMisir Thanks Buddy.. But can You tell me, What is the 1 in the Line

final streak = Hive.box('streaks').get(1); // assuming streaks box is already opened using (Hive.openBox)

themisir commented 3 years ago

What is the 1 in the Line

Ignore that line. The answer to your question is streak.save() part.

shaadart commented 3 years ago

Thanks alot It works, I was Drumming my head with a hammer, although I will recover from the scratches, (no worries) , Because of you, I really tackled this, Love You @TheMisir ❤️