Skip to content

All the errors and some hints shown by the Flutter Linter were fixed. #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import 'package:movies_flutter/scoped_models/app_model.dart';
import 'package:scoped_model/scoped_model.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
Future<void> main() async {
final SharedPreferences sharedPreferences = await SharedPreferences.getInstance();

runApp(ScopedModel<AppModel>(
model: AppModel(sharedPreferences), child: CinematicApp()));
Expand Down
4 changes: 2 additions & 2 deletions lib/model/cast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class Actor {
String profilePicture;
int id;

get profilePictureUrl =>
getMediumPictureUrl((profilePicture != null ? profilePicture : ""));
String get profilePictureUrl =>
getMediumPictureUrl(profilePicture != null ? profilePicture : "");

Actor.fromJson(Map jsonMap)
: character = jsonMap['character'],
Expand Down
2 changes: 1 addition & 1 deletion lib/model/episode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Episode {
: title = jsonMap['name'],
overview = jsonMap['overview'],
stillPath = jsonMap['still_path'] ?? "",
voteAverage = jsonMap['vote_average'] as double ?? 0.0,
voteAverage = jsonMap['vote_average'] ?? 0.0,
episodeNumber = jsonMap['episode_number'],
airDate = jsonMap['air_date'];
}
32 changes: 18 additions & 14 deletions lib/model/mediaitem.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import 'package:movies_flutter/util/utils.dart';

class MediaItem {

MediaItem._internalFromJson(Map jsonMap, {MediaType type= MediaType.movie})
: type = type,
id = jsonMap["id"].toInt(),
voteAverage = jsonMap["vote_average"].toDouble(),
title = jsonMap[(type == MediaType.movie ? "title" : "name")],
posterPath = jsonMap["poster_path"] ?? "",
backdropPath = jsonMap["backdrop_path"] ?? "",
overview = jsonMap["overview"],
releaseDate = jsonMap[
(type == MediaType.movie ? "release_date" : "first_air_date")],
genreIds = (jsonMap["genre_ids"] as List<dynamic>)
.map<int>((dynamic value) => value.toInt())
.toList();

MediaType type;
int id;
double voteAverage;
Expand All @@ -11,6 +26,8 @@ class MediaItem {
String releaseDate;
List<int> genreIds;



String getBackDropUrl() => getLargePictureUrl(backdropPath);

String getPosterUrl() => getMediumPictureUrl(posterPath);
Expand All @@ -24,21 +41,8 @@ class MediaItem {
factory MediaItem(Map jsonMap, MediaType type) =>
MediaItem._internalFromJson(jsonMap, type: type);

MediaItem._internalFromJson(Map jsonMap, {MediaType type: MediaType.movie})
: type = type,
id = jsonMap["id"].toInt(),
voteAverage = jsonMap["vote_average"].toDouble(),
title = jsonMap[(type == MediaType.movie ? "title" : "name")],
posterPath = jsonMap["poster_path"] ?? "",
backdropPath = jsonMap["backdrop_path"] ?? "",
overview = jsonMap["overview"],
releaseDate = jsonMap[
(type == MediaType.movie ? "release_date" : "first_air_date")],
genreIds = (jsonMap["genre_ids"] as List<dynamic>)
.map<int>((value) => value.toInt())
.toList();

Map toJson() => {
Map toJson() => <String, dynamic>{
'type': type == MediaType.movie ? 1 : 0,
'id': id,
'vote_average': voteAverage,
Expand Down
3 changes: 2 additions & 1 deletion lib/model/tvseason.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class TvSeason {
}

String getFormattedTitle() {
if (seasonNumber == 0) return 'Extras';
if (seasonNumber == 0)
return 'Extras';
return 'Season $seasonNumber (${getReleaseYear()})';
}

Expand Down
18 changes: 10 additions & 8 deletions lib/scoped_models/app_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ import 'package:scoped_model/scoped_model.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AppModel extends Model {
final SharedPreferences _sharedPrefs;
Set<MediaItem> _favorites = Set();
static const _THEME_KEY = "theme_prefs_key";
static const _FAVORITES_KEY = "media_favorites_key";

AppModel(this._sharedPrefs) {
_currentTheme = _sharedPrefs.getInt(_THEME_KEY) ?? 0;
_favorites.addAll(_sharedPrefs
.getStringList(_FAVORITES_KEY)
?.map((value) => MediaItem.fromPrefsJson(json.decode(value))) ??
.getStringList(_FAVORITES_KEY)
?.map((dynamic value) => MediaItem.fromPrefsJson(json.decode(value))) ??
Set());
}

static List<ThemeData> _themes = [ThemeData.dark(), ThemeData.light()];
final SharedPreferences _sharedPrefs;
final Set<MediaItem> _favorites = Set();
static const _THEME_KEY = "theme_prefs_key";
static const _FAVORITES_KEY = "media_favorites_key";


static final List<ThemeData> _themes = [ThemeData.dark(), ThemeData.light()];
int _currentTheme = 0;

ThemeData get theme => _themes[_currentTheme];
Expand All @@ -43,7 +45,7 @@ class AppModel extends Model {
1 ??
false;

// TODO: this should ideally be stored in a database and accessed through a repository
// TODO(username): this should ideally be stored in a database and accessed through a repository
void toggleFavorites(MediaItem favoriteItem) {
!isItemFavorite(favoriteItem)
? _favorites.add(favoriteItem)
Expand Down
76 changes: 39 additions & 37 deletions lib/util/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,112 +10,114 @@ import 'package:movies_flutter/model/tvseason.dart';
import 'package:movies_flutter/util/constants.dart';

class ApiClient {
static final _client = ApiClient._internal();
final _http = HttpClient();

factory ApiClient() => _client;

ApiClient._internal();

static final _client = ApiClient._internal();
final HttpClient _http = HttpClient();

final String baseUrl = 'api.themoviedb.org';

factory ApiClient() => _client;

Future<dynamic> _getJson(Uri uri) async {
var response = await (await _http.getUrl(uri)).close();
var transformedResponse = await response.transform(utf8.decoder).join();
final response = await (await _http.getUrl(uri)).close();
final transformedResponse = await response.transform(utf8.decoder).join();
return json.decode(transformedResponse);
}

Future<List<MediaItem>> fetchMovies(
{int page: 1, String category: "popular"}) async {
var url = Uri.https(baseUrl, '3/movie/$category',
{int page= 1, String category= "popular"}) async {
final url = Uri.https(baseUrl, '3/movie/$category',
{'api_key': API_KEY, 'page': page.toString()});

return _getJson(url).then((json) => json['results']).then((data) => data
.map<MediaItem>((item) => MediaItem(item, MediaType.movie))
return _getJson(url).then<dynamic>((dynamic json) => json['results']).then((dynamic data) => data
.map<MediaItem>((dynamic item) => MediaItem(item, MediaType.movie))
.toList());
}

Future<List<MediaItem>> getSimilarMedia(int mediaId,
{String type: "movie"}) async {
var url = Uri.https(baseUrl, '3/$type/$mediaId/similar', {
{String type= "movie"}) async {
final url = Uri.https(baseUrl, '3/$type/$mediaId/similar', {
'api_key': API_KEY,
});

return _getJson(url).then((json) => json['results']).then((data) => data
.map<MediaItem>((item) => MediaItem(
return _getJson(url).then<dynamic>((dynamic json) => json['results']).then((dynamic data) => data
.map<MediaItem>((dynamic item) => MediaItem(
item, (type == "movie") ? MediaType.movie : MediaType.show))
.toList());
}

Future<List<MediaItem>> getMoviesForActor(int actorId) async {
var url = Uri.https(baseUrl, '3/discover/movie', {
final url = Uri.https(baseUrl, '3/discover/movie', {
'api_key': API_KEY,
'with_cast': actorId.toString(),
'sort_by': 'popularity.desc'
});

return _getJson(url).then((json) => json['results']).then((data) => data
.map<MediaItem>((item) => MediaItem(item, MediaType.movie))
return _getJson(url).then<dynamic>((dynamic json) => json['results']).then((dynamic data) => data
.map<MediaItem>((dynamic item) => MediaItem(item, MediaType.movie))
.toList());
}

Future<List<MediaItem>> getShowsForActor(int actorId) async {
var url = Uri.https(baseUrl, '3/person/$actorId/tv_credits', {
final url = Uri.https(baseUrl, '3/person/$actorId/tv_credits', {
'api_key': API_KEY,
});

return _getJson(url).then((json) => json['cast']).then((data) => data
.map<MediaItem>((item) => MediaItem(item, MediaType.show))
return _getJson(url).then<dynamic>((dynamic json) => json['cast']).then((dynamic data) => data
.map<MediaItem>((dynamic item) => MediaItem(item, MediaType.show))
.toList());
}

Future<List<Actor>> getMediaCredits(int mediaId,
{String type: "movie"}) async {
var url =
{String type= "movie"}) async {
final url =
Uri.https(baseUrl, '3/$type/$mediaId/credits', {'api_key': API_KEY});

return _getJson(url).then((json) =>
json['cast'].map<Actor>((item) => Actor.fromJson(item)).toList());
return _getJson(url).then((dynamic json) =>
json['cast'].map<Actor>((dynamic item) => Actor.fromJson(item)).toList());
}

Future<dynamic> getMediaDetails(int mediaId, {String type: "movie"}) async {
var url = Uri.https(baseUrl, '3/$type/$mediaId', {'api_key': API_KEY});
Future<dynamic> getMediaDetails(int mediaId, {String type= "movie"}) async {
final url = Uri.https(baseUrl, '3/$type/$mediaId', {'api_key': API_KEY});

return _getJson(url);
}

Future<List<TvSeason>> getShowSeasons(int showId) async {
var detailJson = await getMediaDetails(showId, type: 'tv');
final dynamic detailJson = await getMediaDetails(showId, type: 'tv');
return detailJson['seasons']
.map<TvSeason>((item) => TvSeason.fromMap(item))
.map<TvSeason>((dynamic item) => TvSeason.fromMap(item))
.toList();
}

Future<List<SearchResult>> getSearchResults(String query) {
var url = Uri
final url = Uri
.https(baseUrl, '3/search/multi', {'api_key': API_KEY, 'query': query});

return _getJson(url).then((json) => json['results']
.map<SearchResult>((item) => SearchResult.fromJson(item))
return _getJson(url).then((dynamic json) => json['results']
.map<SearchResult>((dynamic item) => SearchResult.fromJson(item))
.toList());
}

Future<List<MediaItem>> fetchShows(
{int page: 1, String category: "popular"}) async {
var url = Uri.https(baseUrl, '3/tv/$category',
{int page= 1, String category= "popular"}) async {
final url = Uri.https(baseUrl, '3/tv/$category',
{'api_key': API_KEY, 'page': page.toString()});

return _getJson(url).then((json) => json['results']).then((data) => data
.map<MediaItem>((item) => MediaItem(item, MediaType.show))
return _getJson(url).then<dynamic>((dynamic json) => json['results']).then((dynamic data) => data
.map<MediaItem>((dynamic item) => MediaItem(item, MediaType.show))
.toList());
}

Future<List<Episode>> fetchEpisodes(int showId, int seasonNumber) {
var url = Uri.https(baseUrl, '3/tv/$showId/season/$seasonNumber', {
final url = Uri.https(baseUrl, '3/tv/$showId/season/$seasonNumber', {
'api_key': API_KEY,
});

return _getJson(url).then((json) => json['episodes']).then(
(data) => data.map<Episode>((item) => Episode.fromJson(item)).toList());
return _getJson(url).then<dynamic>((dynamic json) => json['episodes']).then(
(dynamic data) => data.map<Episode>((dynamic item) => Episode.fromJson(item)).toList());
}
}
2 changes: 1 addition & 1 deletion lib/util/constants.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// TODO: To run the app you need an api-key. Get one here: https://developers.themoviedb.org/3/getting-started/introduction
const String API_KEY = <your-api-key>;
const String API_KEY = <your-api-key>;
10 changes: 5 additions & 5 deletions lib/util/mediaproviders.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:movies_flutter/model/mediaitem.dart';
import 'package:movies_flutter/util/api_client.dart';

abstract class MediaProvider {
Future<List<MediaItem>> loadMedia(String category, {int page: 1});
Future<List<MediaItem>> loadMedia(String category, {int page= 1});

Future<List<Actor>> loadCast(int mediaId);

Expand All @@ -17,10 +17,10 @@ abstract class MediaProvider {
class MovieProvider extends MediaProvider {
MovieProvider();

ApiClient _apiClient = ApiClient();
final ApiClient _apiClient = ApiClient();

@override
Future<List<MediaItem>> loadMedia(String category, {int page: 1}) {
Future<List<MediaItem>> loadMedia(String category, {int page= 1}) {
return _apiClient.fetchMovies(category: category, page: page);
}

Expand All @@ -43,10 +43,10 @@ class MovieProvider extends MediaProvider {
class ShowProvider extends MediaProvider {
ShowProvider();

ApiClient _apiClient = ApiClient();
final ApiClient _apiClient = ApiClient();

@override
Future<List<MediaItem>> loadMedia(String category, {int page: 1}) {
Future<List<MediaItem>> loadMedia(String category, {int page= 1}) {
return _apiClient.fetchShows(category: category, page: page);
}

Expand Down
16 changes: 8 additions & 8 deletions lib/util/navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ import 'package:movies_flutter/widgets/media_detail/media_detail.dart';
import 'package:movies_flutter/widgets/search/search_page.dart';
import 'package:movies_flutter/widgets/season_detail/season_detail_screen.dart';

goToMovieDetails(BuildContext context, MediaItem movie) {
MediaProvider provider =
void goToMovieDetails(BuildContext context, MediaItem movie) {
final MediaProvider provider =
(movie.type == MediaType.movie) ? MovieProvider() : ShowProvider();
_pushWidgetWithFade(context, MediaDetailScreen(movie, provider));
}

goToSeasonDetails(BuildContext context, MediaItem show, TvSeason season) =>
void goToSeasonDetails(BuildContext context, MediaItem show, TvSeason season) =>
_pushWidgetWithFade(context, SeasonDetailScreen(show, season));

goToActorDetails(BuildContext context, Actor actor) {
void goToActorDetails(BuildContext context, Actor actor) {
_pushWidgetWithFade(context, ActorDetailScreen(actor));
}

goToSearch(BuildContext context) {
void goToSearch(BuildContext context) {
_pushWidgetWithFade(context, SearchScreen());
}

goToFavorites(BuildContext context) {
void goToFavorites(BuildContext context) {
_pushWidgetWithFade(context, FavoriteScreen());
}

_pushWidgetWithFade(BuildContext context, Widget widget) {
Navigator.of(context).push(
void _pushWidgetWithFade(BuildContext context, Widget widget) {
Navigator.of(context).push<Widget>(
PageRouteBuilder(
transitionsBuilder:
(context, animation, secondaryAnimation, child) =>
Expand Down
Loading