Explanation of the questions is not expanded

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật