fetchDynamicWithAnswers method
Implementation
Future<DynamicFormModel?> fetchDynamicWithAnswers(int typeId) async {
try {
QueryResult<Object?>? result = await _graphqlService.performQuery(
GETDynamicForms.getDynamicFormsJSON,
{"typeId": typeId},
);
print('Query Result: ${result?.data}');
if (result == null || result.hasException) {
print('Query result object is null or has exception');
if (result?.hasException == true) {
print('Exception while fetching dynamic forms: ${result?.exception}');
}
return null;
}
var data = result.data;
if (data == null) {
print('Result data is null');
return null;
}
var dynamicFormsData = data['dynamicForms'] as List<dynamic>?;
if (dynamicFormsData == null || dynamicFormsData.isEmpty) {
print('No dynamic forms found for the given typeId.');
return null;
}
// Parse the first form object
var firstForm = dynamicFormsData.first;
if (firstForm == null) {
print('First form is null');
return null;
}
return DynamicFormModel.fromJson(
Map<String, dynamic>.from(firstForm), Map<String, dynamic>());
} catch (e, stackTrace) {
print('Unexpected error occurred: $e');
print('Stack Trace: $stackTrace');
return null;
}
}