I’m working on a Flutter app and need to fetch movie data from an external API. I keep getting type casting errors when trying to make HTTP requests. Been struggling with this for days now and can’t figure out what’s wrong.
Here’s my API service class:
class ApiService {
static Future<Map<String, dynamic>> fetchMovieData(Map<String, String> params, String baseUrl) async {
Map<String, dynamic> response = {};
final requestHeaders = {
"Content-Type": "application/json",
"X-RapidAPI-Key": "YOUR_API_KEY_HERE",
"X-RapidAPI-Host": "movie-database-imdb-alternative.p.rapidapi.com"
};
try {
final requestUri = Uri.https(baseUrl, "/", params);
final apiResponse = await http.get(requestUri, headers: requestHeaders);
Map<String, dynamic> jsonData = json.decode(apiResponse.body);
response['success'] = jsonData;
return response;
} catch (error) {
response['failure'] = 'Error occurred: ' + error.toString();
return response;
}
}
}
This is how I’m trying to use it:
Map<String, dynamic> apiResult = {};
Map<String, String> movieParams = {
'i': 'tt12361974',
'r': 'json',
'plot': 'full'
};
String apiUrl = 'movie-database-imdb-alternative.p.rapidapi.com';
apiResult = await ApiService.fetchMovieData(movieParams, apiUrl);
I get this error message: type '_InternalLinkedHashMap<String, Object>' is not a subtype of type 'Map<String, String>?'
The API should return movie details in JSON format. What am I doing wrong with the type casting?
The issue you’re encountering stems from a fundamental misunderstanding of how HTTP status codes work. Your code assumes every API call will return valid JSON, but HTTP requests can fail for various reasons - network timeouts, 404 errors, authentication failures, etc. I had this exact problem when I started working with external APIs in Flutter. The type casting error occurs because you’re not checking the response status before attempting to decode the JSON. When the API returns an error response, the JSON structure might be completely different from what you expect, causing the type mismatch. Add a status code check before your json.decode line. Something like if (apiResponse.statusCode == 200)
should resolve this. Also, your error handling needs improvement - you’re concatenating strings with the + operator instead of using string interpolation. The API might also be returning different data types in the JSON response than what your Map expects, so consider logging the raw response body first to see the actual structure being returned.
I encountered something similar when working with movie APIs and the root cause was actually in the response parsing logic. The error suggests that the API response contains nested objects or arrays that don’t match your expected Map<String, String> type structure. Movie API responses typically include complex nested data like arrays of actors, genres, ratings from different sources etc. Your current code tries to force everything into a simple string-to-string mapping which fails when the JSON contains objects or arrays. I recommend using a proper model class with fromJson constructor instead of raw Map types. Also worth noting that your current error handling returns a response map even when the request fails, which means your calling code might try to process invalid data. Consider throwing the exception or returning null on failure rather than wrapping errors in a success-like response structure. The type casting error will disappear once you align your data models with the actual API response structure.
looks like your mixing up the parameter types mate. the Uri.https method expects the queryParameters as Map<String, dynamic> not Map<String, String>. try changing your movieParams declaration or convert it when passing to Uri.https. also check if your api key is working properly cause that error usually pops up when the response structure isnt what you expect