I’m working on a Flutter app that needs to fetch movie information from an external API. I keep running into the same error and can’t figure out what’s wrong. The error message says something about type mismatch with maps. Here’s my API service class:
class ApiService {
static Future<Map<String, dynamic>> fetchMovieData(String movieId, String apiUrl) async {
Map<String, dynamic> responseData = {};
final requestHeaders = {
"Content-Type": "application/json",
"X-RapidAPI-Key": "YOUR_API_KEY_HERE",
"X-RapidAPI-Host": "movie-db-api.p.rapidapi.com"
};
Map<String, String> params = {
'id': movieId,
'format': 'json',
'details': 'extended'
};
try {
final requestUri = Uri.https(apiUrl, "/", params);
final apiResponse = await http.get(requestUri, headers: requestHeaders);
Map<String, dynamic> parsedResponse = json.decode(apiResponse.body);
responseData['result'] = parsedResponse;
return responseData;
} catch (error) {
responseData['errorMessage'] = 'Request failed: ' + error.toString();
return responseData;
}
}
}
This is how I’m trying to use it:
Map<String, dynamic> apiResult = {};
String targetMovieId = 'tt98765432';
String baseApiUrl = 'movie-db-api.p.rapidapi.com';
apiResult = await ApiService.fetchMovieData(targetMovieId, baseApiUrl);
The error keeps showing up and I’m not sure what’s causing the type issue. Any ideas what might be wrong?