sendFeedBack method
Implementation
Future<Response<FeedbackResponse>> sendFeedBack(
FeedbackRequest request,
) async {
if (!Constants.isApiValid()) {
return Response(
body: FeedbackResponse(success: false, error: 'api key not valid'),
);
}
debugPrint('π Feedback API: Starting request...');
debugPrint('π API URL: ${Constants.feedbackApi}');
debugPrint('π API Key: ${Constants.apiKey}');
return await post<FeedbackResponse>(
Constants.feedbackApi,
request.toJson(),
headers: {'api-key': Constants.apiKey},
decoder: (data) {
try {
debugPrint('π Raw Feedback API Response: $data');
debugPrint('π Response type: ${data.runtimeType}');
// Parse JSON string if needed
dynamic parsedData = data;
if (data is String) {
debugPrint('π Parsing JSON string...');
try {
parsedData = jsonDecode(data);
debugPrint('β
JSON parsed successfully');
debugPrint('π Parsed data type: ${parsedData.runtimeType}');
} catch (e) {
debugPrint('β Failed to parse JSON: $e');
return FeedbackResponse(
success: false,
error: 'Failed to parse response',
);
}
}
// Ensure data is a Map
if (parsedData is! Map<String, dynamic>) {
debugPrint(
'β Invalid data format: expected Map, got ${parsedData.runtimeType}',
);
return FeedbackResponse(
success: false,
error: 'Invalid response format',
);
}
final response = FeedbackResponse.fromJson(parsedData);
debugPrint(
'β
Successfully parsed feedback response: success=${response.success}',
);
return response;
} catch (e) {
debugPrint('β Error in feedback decoder: $e');
return FeedbackResponse(
success: false,
error: 'Failed to process response: $e',
);
}
},
);
}