mirror of
https://gitlab.com/foxixus/neomovies_mobile.git
synced 2025-10-27 22:38:50 +05:00
fix: improve API response parsing with detailed logging
Problem:
- Gray screens on movie details and downloads
- No error messages shown to debug issues
- API response structure not fully validated
Solution:
1. Enhanced Movie.fromJson() parsing:
- Added detailed logging for each parsing step
- Safe genre parsing: handles [{id: 18, name: Drama}]
- Safe date parsing with null checks
- Safe runtime parsing for both movies and TV shows
- Better media type detection (movie vs tv)
- Comprehensive error logging with stack traces
2. Added detailed API logging:
- getMovieById(): Log request URL, response status, body preview
- getTvShowById(): Log request URL, response status, body preview
- Log API response structure (keys, types, unwrapped data)
- Makes debugging much easier
3. Based on backend API structure:
Backend returns: {"success": true, "data": {...}}
Movie fields from TMDB:
- id (number)
- title or name (string)
- genres: [{"id": int, "name": string}]
- release_date or first_air_date (string)
- vote_average (number)
- runtime or episode_run_time (number/array)
- number_of_seasons, number_of_episodes (int, optional)
Logging examples:
- 'Parsing Movie from JSON: [id, title, genres, ...]'
- 'Parsed genres: [Drama, Thriller, Mystery]'
- 'Successfully parsed movie: Fight Club'
- 'Response status: 200'
- 'Movie data keys: [id, title, overview, ...]'
Changes:
- lib/data/models/movie.dart: Complete rewrite with safe parsing
- lib/data/api/neomovies_api_client.dart: Add detailed logging
Result:
✅ Safer JSON parsing with null checks
✅ Detailed error logging for debugging
✅ Handles all edge cases from API
✅ Easy to debug gray screen issues via logs
Next steps:
Test the app and check Flutter debug console for:
- API request URLs
- Response bodies
- Parsing errors (if any)
- Successful movie loading messages
This commit is contained in:
@@ -186,17 +186,28 @@ class NeoMoviesApiClient {
|
|||||||
/// Get movie by ID
|
/// Get movie by ID
|
||||||
Future<Movie> getMovieById(String id) async {
|
Future<Movie> getMovieById(String id) async {
|
||||||
final uri = Uri.parse('$apiUrl/movies/$id');
|
final uri = Uri.parse('$apiUrl/movies/$id');
|
||||||
|
print('Fetching movie from: $uri');
|
||||||
final response = await _client.get(uri);
|
final response = await _client.get(uri);
|
||||||
|
|
||||||
|
print('Response status: ${response.statusCode}');
|
||||||
|
print('Response body: ${response.body.substring(0, response.body.length > 500 ? 500 : response.body.length)}...');
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
final apiResponse = json.decode(response.body);
|
final apiResponse = json.decode(response.body);
|
||||||
|
print('Decoded API response type: ${apiResponse.runtimeType}');
|
||||||
|
print('API response keys: ${apiResponse is Map ? apiResponse.keys.toList() : 'Not a map'}');
|
||||||
|
|
||||||
// API returns: {"success": true, "data": {...}}
|
// API returns: {"success": true, "data": {...}}
|
||||||
final movieData = (apiResponse is Map && apiResponse['data'] != null)
|
final movieData = (apiResponse is Map && apiResponse['data'] != null)
|
||||||
? apiResponse['data']
|
? apiResponse['data']
|
||||||
: apiResponse;
|
: apiResponse;
|
||||||
|
|
||||||
|
print('Movie data keys: ${movieData is Map ? movieData.keys.toList() : 'Not a map'}');
|
||||||
|
print('Movie data: $movieData');
|
||||||
|
|
||||||
return Movie.fromJson(movieData);
|
return Movie.fromJson(movieData);
|
||||||
} else {
|
} else {
|
||||||
throw Exception('Failed to load movie: ${response.statusCode}');
|
throw Exception('Failed to load movie: ${response.statusCode} - ${response.body}');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,17 +238,28 @@ class NeoMoviesApiClient {
|
|||||||
/// Get TV show by ID
|
/// Get TV show by ID
|
||||||
Future<Movie> getTvShowById(String id) async {
|
Future<Movie> getTvShowById(String id) async {
|
||||||
final uri = Uri.parse('$apiUrl/tv/$id');
|
final uri = Uri.parse('$apiUrl/tv/$id');
|
||||||
|
print('Fetching TV show from: $uri');
|
||||||
final response = await _client.get(uri);
|
final response = await _client.get(uri);
|
||||||
|
|
||||||
|
print('Response status: ${response.statusCode}');
|
||||||
|
print('Response body: ${response.body.substring(0, response.body.length > 500 ? 500 : response.body.length)}...');
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
final apiResponse = json.decode(response.body);
|
final apiResponse = json.decode(response.body);
|
||||||
|
print('Decoded API response type: ${apiResponse.runtimeType}');
|
||||||
|
print('API response keys: ${apiResponse is Map ? apiResponse.keys.toList() : 'Not a map'}');
|
||||||
|
|
||||||
// API returns: {"success": true, "data": {...}}
|
// API returns: {"success": true, "data": {...}}
|
||||||
final tvData = (apiResponse is Map && apiResponse['data'] != null)
|
final tvData = (apiResponse is Map && apiResponse['data'] != null)
|
||||||
? apiResponse['data']
|
? apiResponse['data']
|
||||||
: apiResponse;
|
: apiResponse;
|
||||||
|
|
||||||
|
print('TV data keys: ${tvData is Map ? tvData.keys.toList() : 'Not a map'}');
|
||||||
|
print('TV data: $tvData');
|
||||||
|
|
||||||
return Movie.fromJson(tvData);
|
return Movie.fromJson(tvData);
|
||||||
} else {
|
} else {
|
||||||
throw Exception('Failed to load TV show: ${response.statusCode}');
|
throw Exception('Failed to load TV show: ${response.statusCode} - ${response.body}');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -68,43 +68,75 @@ class Movie extends HiveObject {
|
|||||||
|
|
||||||
factory Movie.fromJson(Map<String, dynamic> json) {
|
factory Movie.fromJson(Map<String, dynamic> json) {
|
||||||
try {
|
try {
|
||||||
// Parse genres safely
|
print('Parsing Movie from JSON: ${json.keys.toList()}');
|
||||||
|
|
||||||
|
// Parse genres safely - API returns: [{"id": 18, "name": "Drama"}]
|
||||||
List<String> genresList = [];
|
List<String> genresList = [];
|
||||||
if (json['genres'] != null) {
|
if (json['genres'] != null && json['genres'] is List) {
|
||||||
if (json['genres'] is List) {
|
|
||||||
genresList = (json['genres'] as List)
|
genresList = (json['genres'] as List)
|
||||||
.map((g) => g is Map ? (g['name'] as String?) ?? '' : g.toString())
|
.map((g) {
|
||||||
|
if (g is Map && g.containsKey('name')) {
|
||||||
|
return g['name'] as String? ?? '';
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
})
|
||||||
.where((name) => name.isNotEmpty)
|
.where((name) => name.isNotEmpty)
|
||||||
.toList();
|
.toList();
|
||||||
|
print('Parsed genres: $genresList');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse dates safely
|
||||||
|
DateTime? parsedDate;
|
||||||
|
final releaseDate = json['release_date'];
|
||||||
|
final firstAirDate = json['first_air_date'];
|
||||||
|
|
||||||
|
if (releaseDate != null && releaseDate.toString().isNotEmpty && releaseDate.toString() != 'null') {
|
||||||
|
parsedDate = DateTime.tryParse(releaseDate.toString());
|
||||||
|
} else if (firstAirDate != null && firstAirDate.toString().isNotEmpty && firstAirDate.toString() != 'null') {
|
||||||
|
parsedDate = DateTime.tryParse(firstAirDate.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse runtime (movie) or episode_run_time (TV)
|
||||||
|
int? runtimeValue;
|
||||||
|
if (json['runtime'] != null && json['runtime'] is num && (json['runtime'] as num) > 0) {
|
||||||
|
runtimeValue = (json['runtime'] as num).toInt();
|
||||||
|
} else if (json['episode_run_time'] != null && json['episode_run_time'] is List) {
|
||||||
|
final episodeRunTime = json['episode_run_time'] as List;
|
||||||
|
if (episodeRunTime.isNotEmpty && episodeRunTime.first is num) {
|
||||||
|
runtimeValue = (episodeRunTime.first as num).toInt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Movie(
|
// Determine media type
|
||||||
id: (json['id'] as num).toString(), // Ensure id is a string
|
String mediaTypeValue = 'movie';
|
||||||
|
if (json.containsKey('media_type') && json['media_type'] != null) {
|
||||||
|
mediaTypeValue = json['media_type'] as String;
|
||||||
|
} else if (json.containsKey('name') || json.containsKey('first_air_date')) {
|
||||||
|
mediaTypeValue = 'tv';
|
||||||
|
}
|
||||||
|
|
||||||
|
final movie = Movie(
|
||||||
|
id: (json['id'] as num).toString(),
|
||||||
title: (json['title'] ?? json['name'] ?? 'Untitled') as String,
|
title: (json['title'] ?? json['name'] ?? 'Untitled') as String,
|
||||||
posterPath: json['poster_path'] as String?,
|
posterPath: json['poster_path'] as String?,
|
||||||
backdropPath: json['backdrop_path'] as String?,
|
backdropPath: json['backdrop_path'] as String?,
|
||||||
overview: json['overview'] as String?,
|
overview: json['overview'] as String?,
|
||||||
releaseDate: json['release_date'] != null && json['release_date'].toString().isNotEmpty
|
releaseDate: parsedDate,
|
||||||
? DateTime.tryParse(json['release_date'].toString())
|
|
||||||
: json['first_air_date'] != null && json['first_air_date'].toString().isNotEmpty
|
|
||||||
? DateTime.tryParse(json['first_air_date'].toString())
|
|
||||||
: null,
|
|
||||||
genres: genresList,
|
genres: genresList,
|
||||||
voteAverage: (json['vote_average'] as num?)?.toDouble() ?? 0.0,
|
voteAverage: (json['vote_average'] as num?)?.toDouble() ?? 0.0,
|
||||||
popularity: (json['popularity'] as num?)?.toDouble() ?? 0.0,
|
popularity: (json['popularity'] as num?)?.toDouble() ?? 0.0,
|
||||||
runtime: json['runtime'] is num
|
runtime: runtimeValue,
|
||||||
? (json['runtime'] as num).toInt()
|
|
||||||
: (json['episode_run_time'] is List && (json['episode_run_time'] as List).isNotEmpty)
|
|
||||||
? ((json['episode_run_time'] as List).first as num).toInt()
|
|
||||||
: null,
|
|
||||||
seasonsCount: json['number_of_seasons'] as int?,
|
seasonsCount: json['number_of_seasons'] as int?,
|
||||||
episodesCount: json['number_of_episodes'] as int?,
|
episodesCount: json['number_of_episodes'] as int?,
|
||||||
tagline: json['tagline'] as String?,
|
tagline: json['tagline'] as String?,
|
||||||
mediaType: (json['media_type'] ?? (json['title'] != null ? 'movie' : 'tv')) as String,
|
mediaType: mediaTypeValue,
|
||||||
);
|
);
|
||||||
} catch (e) {
|
|
||||||
print('Error parsing Movie from JSON: $e');
|
print('Successfully parsed movie: ${movie.title}');
|
||||||
|
return movie;
|
||||||
|
} catch (e, stackTrace) {
|
||||||
|
print('❌ Error parsing Movie from JSON: $e');
|
||||||
|
print('Stack trace: $stackTrace');
|
||||||
print('JSON data: $json');
|
print('JSON data: $json');
|
||||||
rethrow;
|
rethrow;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user