Trying to convert API Json to Object model but getting this error:
value of type 'Result' can't be returned from the method 'getData' because it has a return type of 'Future<List<Result>?>
api.dart
:
import 'dart:convert' as convert;
import 'package:http/http.dart' as http;
import 'package:movieapp/model/result.dart';
class ApiServices {
Future<List<Result>?> getData() async {
var client = http.Client();
const String apiKey = 'xxx';
const String accessToken =
'xxx';
var url = Uri.parse(
'https://api.themoviedb.org/3/trending/movie/week?api_key=$apiKey');
var response = await client.get(url);
if (response.statusCode == 200) {
var json = response.body;
return Result.fromJson(json);
} else {
print('Request failed with status: ${response.statusCode}.');
}
}
}
Used the Jsontoclass Flutter package
class MovieTredningModel {
final int page;
final List<Result> results;
final int total_pages;
final int total_results;
MovieTredningModel({
required this.page,
required this.results,
required this.total_pages,
required this.total_results,
});
MovieTredningModel copyWith({
int? page,
List<Result>? results,
int? total_pages,
int? total_results,
}) {
return MovieTredningModel(
page: page ?? this.page,
results: results ?? this.results,
total_pages: total_pages ?? this.total_pages,
total_results: total_results ?? this.total_results,
);
}
Map<String, dynamic> toMap() {
return <String, dynamic>{
'page': page,
'results': results.map((x) => x.toMap()).toList(),
'total_pages': total_pages,
'total_results': total_results,
};
}
factory MovieTredningModel.fromMap(Map<String, dynamic> map) {
return MovieTredningModel(
page: map['page'].toInt() as int,
results: List<Result>.from((map['results'] as List<int>).map<Result>((x) => Result.fromMap(x as Map<String,dynamic>),),),
total_pages: map['total_pages'].toInt() as int,
total_results: map['total_results'].toInt() as int,
);
}
String toJson() => json.encode(toMap());
factory MovieTredningModel.fromJson(String source) => MovieTredningModel.fromMap(json.decode(source) as Map<String, dynamic>);
@override
String toString() {
return 'MovieTredningModel(page: $page, results: $results, total_pages: $total_pages, total_results: $total_results)';
}
@override
bool operator ==(covariant MovieTredningModel other) {
if (identical(this, other)) return true;
return
other.page == page &&
listEquals(other.results, results) &&
other.total_pages == total_pages &&
other.total_results == total_results;
}
@override
int get hashCode {
return page.hashCode ^
results.hashCode ^
total_pages.hashCode ^
total_results.hashCode;
}
}
and Model class:
import 'dart:convert';
import 'package:flutter/foundation.dart';
class Result {
final bool adult;
final String backdrop_path;
final int id;
final String title;
final String original_language;
final String original_title;
final String overview;
final String poster_path;
final String media_type;
final List<int> genre_ids;
final double popularity;
final String release_date;
final bool video;
final double vote_average;
final int vote_count;
Result({
required this.adult,
required this.backdrop_path,
required this.id,
required this.title,
required this.original_language,
required this.original_title,
required this.overview,
required this.poster_path,
required this.media_type,
required this.genre_ids,
required this.popularity,
required this.release_date,
required this.video,
required this.vote_average,
required this.vote_count,
});
Map<String, dynamic> toMap() {
return <String, dynamic>{
'adult': adult,
'backdrop_path': backdrop_path,
'id': id,
'title': title,
'original_language': original_language,
'original_title': original_title,
'overview': overview,
'poster_path': poster_path,
'media_type': media_type,
'genre_ids': genre_ids,
'popularity': popularity,
'release_date': release_date,
'video': video,
'vote_average': vote_average,
'vote_count': vote_count,
};
}
factory Result.fromMap(Map<String, dynamic> map) {
return Result(
adult: map['adult'] as bool,
backdrop_path: map['backdrop_path'] as String,
id: map['id'].toInt() as int,
title: map['title'] as String,
original_language: map['original_language'] as String,
original_title: map['original_title'] as String,
overview: map['overview'] as String,
poster_path: map['poster_path'] as String,
media_type: map['media_type'] as String,
genre_ids: List<int>.from((map['genre_ids'] as List<int>)),
popularity: map['popularity'].toDouble() as double,
release_date: map['release_date'] as String,
video: map['video'] as bool,
vote_average: map['vote_average'].toDouble() as double,
vote_count: map['vote_count'].toInt() as int,
);
}
String toJson() => json.encode(toMap());
factory Result.fromJson(String source) =>
Result.fromMap(json.decode(source) as Map<String, dynamic>);
@override
String toString() {
return 'Result(adult: $adult, backdrop_path: $backdrop_path, id: $id, title: $title, original_language: $original_language, original_title: $original_title, overview: $overview, poster_path: $poster_path, media_type: $media_type, genre_ids: $genre_ids, popularity: $popularity, release_date: $release_date, video: $video, vote_average: $vote_average, vote_count: $vote_count)';
}
@override
bool operator ==(covariant Result other) {
if (identical(this, other)) return true;
return other.adult == adult &&
other.backdrop_path == backdrop_path &&
other.id == id &&
other.title == title &&
other.original_language == original_language &&
other.original_title == original_title &&
other.overview == overview &&
other.poster_path == poster_path &&
other.media_type == media_type &&
listEquals(other.genre_ids, genre_ids) &&
other.popularity == popularity &&
other.release_date == release_date &&
other.video == video &&
other.vote_average == vote_average &&
other.vote_count == vote_count;
}
@override
int get hashCode {
return adult.hashCode ^
backdrop_path.hashCode ^
id.hashCode ^
title.hashCode ^
original_language.hashCode ^
original_title.hashCode ^
overview.hashCode ^
poster_path.hashCode ^
media_type.hashCode ^
genre_ids.hashCode ^
popularity.hashCode ^
release_date.hashCode ^
video.hashCode ^
vote_average.hashCode ^
vote_count.hashCode;
}
}
API RESPONSE :
{"page":1,"results":[{"adult":false,"backdrop_path":"/yYrvN5WFeGYjJnRzhY0QXuo4Isw.jpg","id":505642,"title":"Black Panther: Wakanda Forever","original_language":"en","original_title":"Black Panther: Wakanda Forever","overview":"Queen Ramonda, Shuri, M’Baku, Okoye and the Dora Milaje fight to protect their nation from intervening world powers in the wake of King T’Challa’s death. As the Wakandans strive to embrace their next chapter, the heroes must band together with the help of War Dog Nakia and Everett Ross and forge a new path for the kingdom of Wakanda.","poster_path":"/sv1xJUazXeYqALzczSZ3O6nkH75.jpg","media_type":"movie","genre_ids":[28,12,878],"popularity":3864.722,"release_date":"2022-11-09","video":false,"vote_average":7.492,"vote_count":905},{"adult":false,"backdrop_path":"/bQXAqRx2Fgc46uCVWgoPz5L5Dtr.jpg","id":436270,"title":"Black Adam","original_language":"en","original_title":"Black Adam","overview":"Nearly 5,000 years after he was bestowed with the almighty powers of the Egyptian gods—a
Trying to convert API Json to Object model but getting this error. Trying to convert API Json to Object model but getting this errorTrying to convert API Json to Object model but getting this errorTrying to convert API Json to Object model but getting this error
CodePudding user response:
Your api response contain a Map
and inside that has result
list that you want, so change this :
factory Result.fromJson(String source) =>
Result.fromMap(json.decode(source) as Map<String, dynamic>);
to this:
static List<Result> fromJson(String source) => (json.decode(source)["results"] as List).map((e) => Result.fromMap(e as Map<String, dynamic>)).toList();
also in Result.fromMap change this:
genre_ids: List<int>.from((map['genre_ids'] as List<int>)),
to this:
genre_ids: List<int>.from((map['genre_ids'])),