Updating list in users collection

I am actually creating a quiz app which can create quizes and users can attempt those quizes.

So I have a collection called quiz which stores the question and 4 sub-collections called q_a,q_b,q_c and q_d which has an option field to store the option and a is_true field which stores true if it is the right option.

Now I have a page called quiz_page for displaying the questions in a page view where each option is a component and it has 3 parameters questionNum,questionName and isTrue which is set according to to values stored in the quiz document.

I want to save the answers marked by the user by creating a list field called answeredOption. But the there is a possibility that the user will go back to the previous question and change his answer so I want to update the value stored in the list field at a particular index.So i have created a custom action called updateListAtIndex which updates the list field in users collection. But when I run the project the answer not getting updated.

All the actions a defined on a check box of the option component(See the actions below)

CODE FOR THE CUSTOM ACTION :

// Automatic FlutterFlow imports

import '/backend/backend.dart';

import '/flutter_flow/flutter_flow_theme.dart';

import '/flutter_flow/flutter_flow_util.dart';

import '/custom_code/actions/index.dart'; // Imports other custom actions

import '/flutter_flow/custom_functions.dart'; // Imports custom functions

import 'package:flutter/material.dart';

// Begin custom action code

// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

//import '/flutter_flow/flutter_flow_util.dart';

import 'package:cloud_firestore/cloud_firestore.dart';

import 'package:firebase_auth/firebase_auth.dart';

Future updateListAtIndex(

int index, String newValue, String listFieldName) async {

final User? user = FirebaseAuth.instance.currentUser;

if (user != null) {

final String documentId = user.uid;

final DocumentReference documentRef =

FirebaseFirestore.instance.doc(documentId);

// Retrieve the authenticated user's document

final DocumentSnapshot documentSnapshot = await documentRef.get();

if (documentSnapshot.exists) {

// Get the current list value

final data = documentSnapshot.data() as Map<String, dynamic>?;

List<dynamic>? list = data?[listFieldName]?.cast<dynamic>();

if (list != null) {

// Update the desired index in the list

if (index >= 0 && index < list.length) {

list[index] = newValue;

// Update the document with the modified list field

await documentRef.update({listFieldName: list});

} else {

// Handle invalid index

print('Invalid index provided');

}

} else {

// Handle list field not found

print('List field not found');

}

} else {

// Handle document not found

print('Document not found');

}

} else {

// Handle user not authenticated

print('User not authenticated');

}

}