In my trivia app I open the hive box in the main function and then initializing a List with questions from the hive box.
Opening the box :
Future<void> main() async {
await Hive.initFlutter();
Hive.registerAdapter(QuestionAdapter());
questionBox = await Hive.openBox<Question>('capital_questions');
await addQuestionsToBox();
await addQuestionsToList();
printQuestionList();
runApp(const MyApp());
}
Updating a single question :
Future<void> updateQuestionInBox() async{
int amount = question.amountOfTries;
debugPrint("amount $amount");
question.increaseAmountOfTries();
amount = question.amountOfTries;
debugPrint(question.toString());
await questionBox.put(question.questionText, question);
Question debugQuestion = await questionBox.get(question.questionText);
debugPrint(debugQuestion.toString());
}
In this function I am updating the amountOfTries for the question, updating it in the box and then getting it from the box to see that the box has the updated question, the box contains the updated question.
When i stop the app and run it again the question has amountOfTries =0 despite the fact it was 1 before i closed the app.
I thought it was something with the from/toJson but I realized it is not needed in my code. Also tried re-constructing the adapter file but it didn’t work.
Totah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.