submitQuiz function
Implementation
Future<ResponseReward?> submitQuiz(
Responses quizResponse,
String taskId,
String? clientId,
String userId,
) async {
var headers = {
'Content-Type': 'application/json',
'apiKey': Nudge.getInstance().apiKey,
};
var data = {
"uid": userId,
"session_id":Nudge.sessionId,
"task_id": taskId,
'responses': quizResponse.responses.map((e) => e.toJson()).toList(),
"occurred_at": DateTime.now().toIso8601String(),
};
log("Data: $data", name: 'submitQuiz');
var response = await http.post(
Uri.parse('$nudgeUrl/sdk/submit'),
headers: headers,
body: jsonEncode(data),
);
log("Response: ${response.body}, ${response.headers}", name: 'submitQuiz');
if (response.statusCode == 200) {
if (jsonDecode(response.body)["rewards"].isEmpty) {
return null;
}
return ResponseReward.fromJson(jsonDecode(response.body)["rewards"][0]);
} else {
throw Exception(
'Failed to submit quiz. Status code: ${response.statusCode}');
}
}