I am working on quiz app, I want that if user has answered the questions then after refreshing the app explanation should be expanded , i wrote this code which is not working, can anyone help me
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:usml_question_of_day/models/categories_model.dart';
import 'package:usml_question_of_day/models/option_percentage_model.dart';
import 'package:usml_question_of_day/view/questionPage/service/question_service.dart';
class QuestionController extends GetxController with QuestionService {
RxBool isLoading = true.obs;
RxString selectedAnswerValue = "".obs;
RxBool isSelected = false.obs;
RxList<CategoryQuestions> allQuestions = <CategoryQuestions>[].obs;
RxList<String> questionCategoryName = <String>[].obs;
RxList<int> questionCategoryId = <int>[].obs;
RxInt questionIndex = 0.obs;
RxString categoryName = "".obs;
RxBool isSnackbarShown = false.obs;
SharedPreferences? _prefs;
Set<int> addedQuestionIds = <int>{}; // Track added question IDs
@override
void onInit() {
super.onInit();
_initializePreferences();
}
Future<void> _initializePreferences() async {
_prefs = await SharedPreferences.getInstance();
await fetchData(); // Fetch data before loading saved state
_loadSavedData();
}
void _loadSavedData() {
for (var question in allQuestions) {
final questionId = question.id!;
String? savedAnswer = _prefs?.getString('selectedAnswer_$questionId');
bool savedExplanationExpanded =
_prefs?.getBool('isExplanationExpanded_$questionId') ??
true; // Default to true
if (savedAnswer != null && savedAnswer.isNotEmpty) {
answeredQuestionId.add(questionId);
answeredSelectedAnswer.add(savedAnswer);
if (questionIndex.value == allQuestions.indexOf(question)) {
selectedAnswerValue.value = savedAnswer;
isSelected.value = true;
}
}
// Set isExplanationExpanded based on saved state, defaulting to true
question.isExplanationExpanded = savedExplanationExpanded;
}
}
void saveSelectedData(int questionId, String answer) {
_prefs?.setString('selectedAnswer_$questionId', answer);
_prefs?.setBool('isExplanationExpanded_$questionId',
allQuestions[questionIndex.value].isExplanationExpanded);
}
Future<void> fetchData() async {
isLoading.value = true;
allQuestions.clear();
addedQuestionIds.clear(); // Clear existing IDs before fetching new data
final dt = await getDataFromApi();
if (dt.categories != null) {
for (var element in dt.categories!) {
questionCategoryName.add(element.name!);
questionCategoryId.add(element.id!);
for (var question in element.categoryQuestions!) {
if (!addedQuestionIds.contains(question.id)) {
question.isExplanationExpanded =
true; // Initialize RxBool for explanation state
allQuestions.add(question);
addedQuestionIds.add(question.id!);
}
}
}
if (allQuestions.isNotEmpty) {
findCatName(0);
questionIndex.listen((p0) {
findCatName(p0);
// Update selected answer and explanation state for the new question index
selectedAnswerValue.value = "";
isSelected.value = false;
if (answeredQuestionId.contains(allQuestions[p0].id!)) {
selectedAnswerValue.value =
_prefs?.getString('selectedAnswer_${allQuestions[p0].id!}') ??
"";
isSelected.value = true;
}
allQuestions[p0].isExplanationExpanded = _prefs
?.getBool('isExplanationExpanded_${allQuestions[p0].id!}') ??
true; // Default to true
});
}
}
isLoading.value = false;
}
void findCatName(int idx) {
final catId = allQuestions[idx].categoryId;
for (int i = 0; i < questionCategoryId.length; i++) {
if (questionCategoryId[i] == catId) {
categoryName.value = questionCategoryName[i];
}
}
}
RxList<int> answeredQuestionId = <int>[].obs;
RxList<String> answeredSelectedAnswer = <String>[].obs;
RxBool shouldCheck = false.obs;
RxBool isExplanation = false.obs;
RxList<List<Percentage>> answeredPercentage = <List<Percentage>>[].obs;
RxBool isnAnswerSubmitted = false.obs;
Future<void> sendAnswer(String optId, String catId) async {
final dt = await submitAnswer(optId, catId);
if (dt.message != null) {
answeredPercentage.add(dt.percentage!);
isExplanation.value = true; // Expand explanation after submitting answer
_prefs?.setBool(
'isExplanationExpanded_${allQuestions[questionIndex.value].id}',
true); // Save explanation expanded state
allQuestions[questionIndex.value].isExplanationExpanded =
true; // Update local state
}
}
RxList<String> favIds = <String>[].obs;
Future<void> addToFav(String qstId, catId) async {
favIds.add(qstId);
await addToFavorites(qstId, catId);
}
bool isFav(String qstId) {
return favIds.contains(qstId);
}
}
I have used Sharedprefrences for storing the correct answer and user selected option
1