Flutter HTTP request throws exception when retrieving data from API service

I’m working on a Flutter app and running into an issue with HTTP requests. Been trying to fix this for a couple days now but can’t seem to get it right. When I make a GET request to fetch movie data, I keep getting an exception about type casting. Here’s my HTTP service class:

class ApiService {
  static Future<Map<String, Object>> fetchDataWithParams(Map<String, dynamic> params, String baseUrl) async {
    Map<String, Object> response = new Map();
    final requestHeaders = {
      "content-type": "application/json",
      "x-rapidapi-key": "YOUR_API_KEY_HERE",
      "x-rapidapi-host": "movie-database-alternative.p.rapidapi.com",
      "useQueryString": true
    };
    try {
      final requestUri = Uri.https(baseUrl, "", params);
      var apiResponse = await http.get(requestUri, headers: requestHeaders);
      Map<String, Object> parsedResponse = jsonDecode(apiResponse.body);
      response['result'] = parsedResponse;
      return response;
    } catch (error) {
      response['error'] = 'Request failed: ' + error.toString();
      return response;
    }
  }
}

This is how I’m calling it:

Map<String, Object> apiResult = new Map();
Map<String, dynamic> searchParams = {
  'i': 'tt12361974',
  'r': 'json',
  'plot': 'full'
};
String apiUrl = 'movie-database-alternative.p.rapidapi.com';
apiResult = await ApiService.fetchDataWithParams(searchParams, apiUrl);

The error I’m getting is:

type '_InternalLinkedHashMap<String, Object>' is not a subtype of type 'Map<String, String>?'

Anyone know what’s causing this type mismatch? The API should return movie details in JSON format but something’s going wrong with the parsing.

your jsonDecode returns Map<String, dynamic> but ur assigning it to Map<String, Object>. change your parsedResponse declaration to Map<String, dynamic> parsedResponse = jsonDecode(apiResponse.body); - that’ll fix the type casting error.

You’re mixing Map<String, Object> and Map<String, dynamic> types, which is causing the problem. Alex covered the jsonDecode issue, but there’s another bug with your Uri.https call. That third parameter needs Map<String, String> for query params, not Map<String, dynamic>. Change your searchParams to Map<String, String>. You might also want to use Uri.https properly or just go with Uri.parse and build the query string manually. I’ve hit this same issue with different APIs - keeping your types consistent from the start saves tons of debugging headaches.

Had this exact issue last month with a similar API setup. The problem’s in your Uri.https constructor - it wants Map<String, String> for query parameters, but you’re giving it Map<String, dynamic>. Even though your values are strings, the compiler doesn’t know that.

Here’s what fixed it for me: convert your params first like this: Map<String, String> stringParams = params.map((key, value) => MapEntry(key, value.toString())); final requestUri = Uri.https(baseUrl, "", stringParams);

This handles the type conversion and stops the casting exception. Also check apiResponse.statusCode before parsing - I kept getting silent failures when the API returned errors but I was still trying to decode the response body.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.