When I try to fetch data from API the follow error shows.
Error : type ‘Null’ is not a subtype of type ‘List<dynamic>’
Here I’m sharing the calling function and model. When I fetch another types API URL using this same model it works perfectly. But in this case it shows the given error. But when I create another model it works.
FutureBuilder(
future: movieObj.fetchMovies(movieType.cast,id: movieData.id),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<MovieModel>movieData = snapshot.data??[];
return layouts(movieData: movieData);
}
return Center(child: CircularProgressIndicator(),);
},
)
Model :
// To parse this JSON data, do
//
// final MovieModel = MovieModelFromJson(jsonString);
import 'dart:convert';
MovieModel MovieModelFromJson(String str) => MovieModel.fromJson(json.decode(str));
String MovieModelToJson(MovieModel data) => json.encode(data.toJson());
class MovieModel {
bool? adult;
String backdropPath;
int? id;
String? originalLanguage;
String? originalTitle;
String? overview;
double? popularity;
String posterPath;
String? releaseDate;
String title;
bool? video;
double? voteAverage;
int? voteCount;
MovieModel({
required this.adult,
required this.backdropPath,
required this.id,
required this.originalLanguage,
required this.originalTitle,
required this.overview,
required this.popularity,
required this.posterPath,
required this.releaseDate,
required this.title,
required this.video,
required this.voteAverage,
required this.voteCount,
});
factory MovieModel.fromJson(Map<String, dynamic> json) => MovieModel(
adult: json["adult"],
backdropPath: json["backdrop_path"],
id: json["id"],
originalLanguage: json["original_language"],
originalTitle: json["original_title"],
overview: json["overview"],
popularity: json["popularity"]?.toDouble(),
posterPath: json["poster_path"],
releaseDate: json["release_date"],
title: json["title"],
video: json["video"],
voteAverage: json["vote_average"]?.toDouble(),
voteCount: json["vote_count"],
);
Map<String, dynamic> toJson() => {
"adult": adult,
"backdrop_path": backdropPath,
"id": id,
"original_language": originalLanguage,
"original_title": originalTitle,
"overview": overview,
"popularity": popularity,
"poster_path": posterPath,
"release_date": releaseDate,
"title": title,
"video": video,
"vote_average": voteAverage,
"vote_count": voteCount,
};
}
Response :
import 'dart:convert';
import 'package:http/http.dart';
import 'package:movie_application/api%20Integration/model/movieModel.dart';
import 'package:movie_application/api%20Integration/model/similarModel.dart';
import 'package:movie_application/constants/apiConstants.dart';
enum movieType {
nowPlaying,
popular,
topRated,
upComing,
similar,
latest,
videos,
cast
}
class movieClass {
Future<List<MovieModel>?> fetchMovies(movieType type, {id}) async {
var url = '';
if (type == movieType.nowPlaying) {
url = movieUrl + CnowPlaying;
} else if (type == movieType.popular) {
url = movieUrl + Cpopular;
} else if (type == movieType.topRated) {
url = movieUrl + CtopRated;
} else if (type == movieType.upComing) {
url = movieUrl + CupComing;
} else if (type == movieType.cast) {
url = movieUrl + id.toString() + Ccast;
} else if (type == movieType.similar) {
url = movieUrl + id.toString() + Csimilar;
} else if (type == movieType.videos) {
url = movieUrl + id.toString() + Cvideos;
}
Response response = await get(Uri.parse(url + '?api_key=3c63914fd23ecbed6507f4396ed68164'));
Map<String,dynamic>jsonBody = jsonDecode(response.body);
List<dynamic>result = jsonBody['results'];
List<MovieModel>resultList = result.map((e) => MovieModel.fromJson(e)).toList();
print(url);
return resultList;
}
}
2
Answers
Possibly sound like the result data is null ?
it seems like that
jsonBody['results']
is a null value, please check if there is aresults
field injsonBody