I’m working on a Flutter project with three main pages: Home, Exam, and TableData. When a user completes an exam, they either navigate to a Certificate page (if all answers are correct) or a TryAgain page (if not all answers are correct).
I have implemented a logic that records the user’s activity in the TableData as soon as they navigate to either Certificate or TryAgain. However, I’m facing an issue where both activities (“Certificate” and “TryAgain”) are being recorded at the same time for the same email, even though I want only one to be logged based on where the user navigates.
Current Problem
Currently, when I navigate, both activities (“Certificate” and “TryAgain”) are being logged at the same time, when I only want one to be logged based on where the user actually navigates.
TableData Widget:
This widget is responsible for displaying the activities that get recorded after navigating from the exam page:
class TableData extends StatelessWidget {
final String userEmail;
final List<UserActivity> activities;
TableData({required this.userEmail, required this.activities});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Data Table')),
body: DataTable(
columns: const <DataColumn>[
DataColumn(label: Text('Email')),
DataColumn(label: Text('Time')),
DataColumn(label: Text('Activity')),
],
rows: activities.map((activity) {
return DataRow(
cells: [
DataCell(Text(activity.email)),
DataCell(Text(DateFormat('yyyy-MM-dd HH:mm').format(activity.time))),
DataCell(Text(activity.page)),
],
);
}).toList(),
),
);
}
}
Navigation Logic in Exam Page:
Here’s the logic I use in the ExamScreen to handle user navigation:
setState(() {
questionsNumber++;
if (questionsNumber >= questionGroup.length) {
DateTime currentTime = DateTime.now(); // Record the time
String pageName;
if (allAnswersCorrect) {
// Navigate to Certificate page
pageName = 'Certificate';
// Add UserActivity to activities list
activities.add(UserActivity(
email: widget.userEmail,
time: currentTime,
page: pageName,
));
Navigator.pushNamed(context, Certificate.screenroute);
} else {
// Navigate to TryAgain page
pageName = 'TryAgain';
// Add UserActivity to activities list
activities.add(UserActivity(
email: widget.userEmail,
time: currentTime,
page: pageName,
));
Navigator.pushNamed(context, Tryagain.screenroute);
}
}
});
How can I modify my logic to ensure that only one activity (either “Certificate” or “TryAgain”) is recorded based on the page where the user actually navigates? What could be causing the issue of both activities being logged at the same time?
1