Line data Source code
1 : import 'dart:convert';
2 :
3 : /// Represents a movie entity with all its attributes.
4 : ///
5 : /// The [TheMovie] class is used to handle movie data, including parsing from JSON, converting to JSON, and creating copies with modified values.
6 : ///
7 : /// ### Properties
8 : /// - [adult]: Indicates if the movie is for adults.
9 : /// - [backdropPath]: Path to the movie's backdrop image.
10 : /// - [genreIds]: List of genre IDs associated with the movie.
11 : /// - [id]: Unique identifier for the movie.
12 : /// - [originalLanguage]: Original language of the movie.
13 : /// - [originalTitle]: Original title of the movie.
14 : /// - [overview]: Brief description of the movie.
15 : /// - [popularity]: Popularity score of the movie.
16 : /// - [posterPath]: Path to the movie's poster image.
17 : /// - [releaseDate]: Release date of the movie.
18 : /// - [title]: Title of the movie.
19 : /// - [video]: Indicates if the movie has a video.
20 : /// - [voteAverage]: Average vote score for the movie.
21 : /// - [voteCount]: Total number of votes for the movie.
22 : ///
23 : /// ### Methods
24 : /// - [copyWith]: Returns a copy of the instance with modified values.
25 : /// - [fromRaw]: Creates an instance from a raw JSON string.
26 : /// - [fromMap]: Creates an instance from a JSON map.
27 : /// - [empty]: Creates an empty instance with default values.
28 : /// - [toMap]: Converts the instance to a JSON map.
29 : ///
30 : /// ### Example
31 : /// ```dart
32 : /// final movie = TheMovie.fromRaw(jsonString);
33 : /// print(movie.title);
34 : /// ```
35 : class TheMovie {
36 : /// Creates an instance of `TheMovie`.
37 : ///
38 : /// **Parameters:**
39 : /// - `adult` (bool): Indicates if the movie is for adults.
40 : /// - `backdropPath` (String): The path to the movie's backdrop image.
41 : /// - `genreIds` (List<int>): A list of genre IDs associated with the movie.
42 : /// - `id` (int): The unique identifier for the movie.
43 : /// - `originalLanguage` (String): The original language of the movie.
44 : /// - `originalTitle` (String): The original title of the movie.
45 : /// - `overview` (String): A brief description of the movie.
46 : /// - `popularity` (double): The popularity score of the movie.
47 : /// - `posterPath` (String): The path to the movie's poster image.
48 : /// - `releaseDate` (String): The release date of the movie.
49 : /// - `title` (String): The title of the movie.
50 : /// - `video` (bool): Indicates if the movie has a video.
51 : /// - `voteAverage` (double): The average vote score for the movie.
52 : /// - `voteCount` (int): The total number of votes for the movie.
53 9 : TheMovie({
54 : required this.adult,
55 : required this.backdropPath,
56 : required this.genreIds,
57 : required this.id,
58 : required this.originalLanguage,
59 : required this.originalTitle,
60 : required this.overview,
61 : required this.popularity,
62 : required this.posterPath,
63 : required this.releaseDate,
64 : required this.title,
65 : required this.video,
66 : required this.voteAverage,
67 : required this.voteCount,
68 : });
69 :
70 : bool adult;
71 : String backdropPath;
72 : List<int> genreIds;
73 : int id;
74 : String originalLanguage;
75 : String originalTitle;
76 : String overview;
77 : double popularity;
78 : String posterPath;
79 : String releaseDate;
80 : String title;
81 : bool video;
82 : double voteAverage;
83 : int voteCount;
84 :
85 : /// Creates a copy of the current `TheMovie` instance with optional modifications.
86 : ///
87 : /// **Parameters:**
88 : /// - `adult` (bool?): The new value for `adult`.
89 : /// - `backdropPath` (String?): The new value for `backdropPath`.
90 : /// - `genreIds` (List<int>?): The new value for `genreIds`.
91 : /// - `id` (int?): The new value for `id`.
92 : /// - `originalLanguage` (String?): The new value for `originalLanguage`.
93 : /// - `originalTitle` (String?): The new value for `originalTitle`.
94 : /// - `overview` (String?): The new value for `overview`.
95 : /// - `popularity` (double?): The new value for `popularity`.
96 : /// - `posterPath` (String?): The new value for `posterPath`.
97 : /// - `releaseDate` (String?): The new value for `releaseDate`.
98 : /// - `title` (String?): The new value for `title`.
99 : /// - `video` (bool?): The new value for `video`.
100 : /// - `voteAverage` (double?): The new value for `voteAverage`.
101 : /// - `voteCount` (int?): The new value for `voteCount`.
102 : ///
103 : /// **Returns:**
104 : /// - A new `TheMovie` instance with the updated values.
105 8 : TheMovie copyWith({
106 : bool? adult,
107 : String? backdropPath,
108 : List<int>? genreIds,
109 : int? id,
110 : String? originalLanguage,
111 : String? originalTitle,
112 : String? overview,
113 : double? popularity,
114 : String? posterPath,
115 : String? releaseDate,
116 : String? title,
117 : bool? video,
118 : double? voteAverage,
119 : int? voteCount,
120 8 : }) => TheMovie(
121 8 : adult: adult ?? this.adult,
122 8 : backdropPath: backdropPath ?? this.backdropPath,
123 8 : genreIds: genreIds ?? this.genreIds,
124 3 : id: id ?? this.id,
125 8 : originalLanguage: originalLanguage ?? this.originalLanguage,
126 8 : originalTitle: originalTitle ?? this.originalTitle,
127 8 : overview: overview ?? this.overview,
128 8 : popularity: popularity ?? this.popularity,
129 8 : posterPath: posterPath ?? this.posterPath,
130 8 : releaseDate: releaseDate ?? this.releaseDate,
131 3 : title: title ?? this.title,
132 8 : video: video ?? this.video,
133 8 : voteAverage: voteAverage ?? this.voteAverage,
134 8 : voteCount: voteCount ?? this.voteCount,
135 : );
136 :
137 : /// Creates a `TheMovie` instance from a raw JSON string.
138 : ///
139 : /// **Parameters:**
140 : /// - `str` (String): The raw JSON string.
141 : ///
142 : /// **Returns:**
143 : /// - A `TheMovie` instance parsed from the JSON string.
144 3 : factory TheMovie.fromRaw(String str) => TheMovie.fromMap(json.decode(str));
145 :
146 : /// Creates a `TheMovie` instance from a JSON map.
147 : ///
148 : /// **Parameters:**
149 : /// - `json` (Map<String, dynamic>): The JSON map.
150 : ///
151 : /// **Returns:**
152 : /// - A `TheMovie` instance parsed from the JSON map.
153 4 : factory TheMovie.fromMap(Map<String, dynamic> json) => TheMovie(
154 2 : adult: json["adult"] ?? false,
155 2 : backdropPath: json["backdrop_path"] ?? '',
156 6 : genreIds: List<int>.from((json["genre_ids"] ?? []).map((x) => x)),
157 2 : id: json["id"] ?? 0,
158 2 : originalLanguage: json["original_language"] ?? '',
159 2 : originalTitle: json["original_title"] ?? '',
160 2 : overview: json["overview"] ?? '',
161 4 : popularity: json["popularity"]?.toDouble() ?? 0.0,
162 2 : posterPath: json["poster_path"] ?? '',
163 2 : releaseDate: json["release_date"] ?? '',
164 2 : title: json["title"] ?? '',
165 2 : video: json["video"] ?? false,
166 4 : voteAverage: json["vote_average"]?.toDouble() ?? 0.0,
167 2 : voteCount: json["vote_count"] ?? 0,
168 : );
169 :
170 : /// Creates an empty `TheMovie` instance.
171 : ///
172 : /// **Returns:**
173 : /// - A `TheMovie` instance with default values.
174 18 : factory TheMovie.empty() => TheMovie(
175 : adult: false,
176 : backdropPath: '',
177 9 : genreIds: [],
178 : id: 0,
179 : originalLanguage: '',
180 : originalTitle: '',
181 : overview: '',
182 : popularity: 0.0,
183 : posterPath: '',
184 : releaseDate: '',
185 : title: '',
186 : video: false,
187 : voteAverage: 0.0,
188 : voteCount: 0,
189 : );
190 :
191 : /// Converts the `TheMovie` instance to a JSON map.
192 : ///
193 : /// **Returns:**
194 : /// - A `Map<String, dynamic>` representing the `TheMovie` instance.
195 4 : Map<String, dynamic> toMap() => {
196 2 : "adult": adult,
197 2 : "backdrop_path": backdropPath,
198 6 : "genre_ids": List<dynamic>.from(genreIds.map((x) => x)),
199 2 : "id": id,
200 2 : "original_language": originalLanguage,
201 2 : "original_title": originalTitle,
202 2 : "overview": overview,
203 2 : "popularity": popularity,
204 2 : "poster_path": posterPath,
205 2 : "release_date": releaseDate,
206 2 : "title": title,
207 2 : "video": video,
208 2 : "vote_average": voteAverage,
209 2 : "vote_count": voteCount,
210 : };
211 : }
|