Trouble with API call in Flutter: Error when fetching movie data

I’m having issues with my Flutter app. I’m trying to get movie info from an API, but I keep running into an error. Here’s what I’m doing:

class MovieFetcher {
  static Future<Map<String, dynamic>> fetchMovieDetails(String movieId) async {
    final apiKey = 'your-api-key-here';
    final apiHost = 'movie-info-service.example.com';
    
    final params = {
      'movie_id': movieId,
      'format': 'json',
      'details': 'full'
    };
    
    final headers = {
      'Content-Type': 'application/json',
      'API-Key': apiKey,
      'API-Host': apiHost
    };
    
    try {
      final uri = Uri.https(apiHost, '/movie', params);
      final response = await http.get(uri, headers: headers);
      return json.decode(response.body);
    } catch (e) {
      print('Error: $e');
      return {'error': e.toString()};
    }
  }
}

When I call this function, I get an error saying:

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

I’m not sure what I’m doing wrong. The API should return movie details like title, year, plot, etc. Any ideas on how to fix this?

I’ve dealt with similar API issues in Flutter before. One thing that often helps is to log the full response body before decoding it. This way, you can see exactly what the API is returning and spot any unexpected data structures.

Try adding this line right after getting the response:

print(‘Response body: ${response.body}’);

This might reveal that the API is sending back an array instead of an object, or maybe there’s an extra wrapper around the data you want. Once you see the actual structure, you can adjust your parsing logic accordingly.

Also, don’t forget to check the response status code. Sometimes APIs return error messages in a 200 OK response, which can trip up your parsing. Add a check like:

if (response.statusCode != 200) {
throw Exception(‘API error: ${response.statusCode}’);
}

These steps have saved me countless hours of debugging. Good luck with your project!

hey mate, i had a similar problem. try using Map<String, dynamic>.from(json.decode(response.body)) instead. this forces the right type. also, double-check ur API key and host - sometimes thats the real culprit. good luck!

I’ve encountered a similar issue before. The error suggests a mismatch between the expected and actual types of the data you’re working with. In this case, it seems the API response isn’t exactly what your code expects.

Try modifying your function to explicitly cast the decoded JSON to Map<String, dynamic>:

return json.decode(response.body) as Map<String, dynamic>;

If that doesn’t work, you might need to adjust how you’re handling the response. Consider using a model class to parse the JSON data instead of working with raw Map objects. This approach can provide better type safety and make your code more robust.

Also, double-check the API documentation to ensure you’re using the correct parameters and headers. Sometimes, slight mismatches in the request structure can lead to unexpected response formats.