API Interceptor Help

Database & APIs

I have an Algolia API call that retrieves product information including the price of an item. The issue I have is that if the price is a whole number, its treated as an integer whereas if there is a decimal, its treated as a double. I wanted to create an interceptor that adds ".00" to a whole number and converts it to a double. I've tried may different functions to accomplish this but i keep getting errors. I've been at this for a few days and would really love some help.

I would like to add that I have tried several types of approaches to getting this resolved, the example below is just one of them.

What have you tried so far?
class PriceInterceptor extends FFApiInterceptor {
  @override
  Future<ApiCallOptions> onRequest({
    required ApiCallOptions options,
  }) async {
    return options; // No modifications to the request
  }

  @override
  Future<ApiCallResponse> onResponse({
    required ApiCallResponse response,
    required Future<ApiCallResponse> Function() retryFn,
  }) async {
    // Ensure response is a valid JSON Map
    if (response.jsonBody is! Map<String, dynamic>) {
      return response; // If not valid JSON, return it as-is
    }

    // Clone the response JSON for safe modification
    final Map<String, dynamic> jsonResponse =
        Map<String, dynamic>.from(response.jsonBody);

    // Ensure 'hits' exists and is a List
    if (jsonResponse.containsKey('hits') && jsonResponse['hits'] is List) {
      final List<dynamic> hits = jsonResponse['hits'];

      for (var product in hits) {
        if (product is Map<String, dynamic> && product.containsKey('price')) {
          final price = product['price'];

          // Keep the original price field unchanged
          // Create a new field called "formattedPrice"
          if (price is int) {
            product['formattedPrice'] =
                "$price.00"; // Convert int to string with .00
          } else if (price is double) {
            product['formattedPrice'] =
                price.toStringAsFixed(2); // Ensure two decimal places
          } else if (price is String) {
            // Handle cases where the price is already a string (edge case)
            try {
              final parsedPrice = double.parse(price);
              product['formattedPrice'] = parsedPrice % 1 == 0
                  ? "${parsedPrice.toInt()}.00"
                  : parsedPrice.toStringAsFixed(2);
            } catch (e) {
              product['formattedPrice'] =
                  price; // Keep it as is if parsing fails
            }
          }
        }
      }
    }

    // Ensure statusCode is an int (correct first argument)
    final int statusCode =
        response.statusCode is int ? response.statusCode : 200;

    // Ensure headers are properly typed as Map<String, String> (correct second argument)
    final Map<String, String> headers = {};
    response.headers?.forEach((key, value) {
      headers[key.toString()] =
          value.toString(); // Convert all values to String
    });

    // Fix the argument order in ApiCallResponse (statusCode must be first)
    return ApiCallResponse(
      statusCode, // First argument: must be an int
      jsonResponse, // Second argument: must be a Map<String, dynamic>
      headers, // Third argument: must be a Map<String, String>
    );
  }
}
Did you check FlutterFlow's Documentation for this topic?
Yes
1