sendFeedBack method

Future<Response<FeedbackResponse>> sendFeedBack(
  1. FeedbackRequest request
)

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',
        );
      }
    },
  );
}