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.