flutterhttp

Why am I seeing the error 'type 'Null' is not a subtype of type 'int'' in my Flutter http API call?


when i run my code have error. the contents of the error "type 'Null' is not a subtype of type 'int'"

Below is my code where the problem occurs


class HttpHelper{
   final String urlKey = 'api_key=62ecff60f88bc51146b725ea8c314092';
   final String urlBase = 'http://api.themoviedb.org/3/movie';
   final String urlUpcoming = '/upcoming?';
   final String urlLanguage = '&language=ko-KR';

   Future<Album> getUpcoming() async{
    final upcoming = urlBase+urlUpcoming+urlKey+urlLanguage;
    final result = await http
        .get(Uri.parse(upcoming));

    if (result.statusCode == 200) {
      return Album.fromJson(jsonDecode(result.body));
    } else {
      throw Exception('Failed to load album');
    }
  }
}


class Album {
   final int id;
   final String title;
   final double voteAverage;
   final String releaseDate;
   final String overview;
   final String posterPath;

    const Album({
    required this.id,
    required this.title,
    required this.voteAverage,
    required this.releaseDate,
    required this.overview,
    required this.posterPath,
  });

    factory Album.fromJson(Map<String, dynamic> json) {
    return Album(
      id: json['id'],
      title: json['title'],
      voteAverage: json['voteAverage'],
      releaseDate: json['releaseDate'],
      overview: json['overview'],
      posterPath: json['posterPath'],
    );
  }
}

I did deleted some filed and constructor, but have same problem


Solution

  • You have numerous typos in your fromJson - see the corrections below. Also, you have to allow for the possibility that vote average being a whole number (in which case the json decoder makes it an int).

    The biggest issue is that your API returns you an array of albums., rather than just one, and that it is under a key of the top JSON object.

    I've shown how to extract the whole list of albums, but just selected the first to avoid changing the method signature.

    Finally, from a style point-of-view, avoid creating pointless classes like HttpHelper. Its single method could just be a top level function.

    if (result.statusCode == 200) {
      final decoded = jsonDecode(result.body);
      final results = decoded['results'] as List<dynamic>;
      final albums = results.map<Album>((e) => Album.fromJson(e)).toList();
      return albums.first;
    }
    
    class Album {
      final int id;
      final String title;
      final double voteAverage;
      final String releaseDate;
      final String overview;
      final String posterPath;
    
      const Album({
        required this.id,
        required this.title,
        required this.voteAverage,
        required this.releaseDate,
        required this.overview,
        required this.posterPath,
      });
    
      factory Album.fromJson(Map<String, dynamic> json) {
        final num va = json['vote_average'];
        return Album(
          id: json['id'],
          title: json['title'],
          voteAverage: va is double ? va : va.toDouble(),
          releaseDate: json['release_date'],
          overview: json['overview'],
          posterPath: json['poster_path'],
        );
      }
    }