Skip to content

Instantly share code, notes, and snippets.

@ghozay19
Created February 16, 2020 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghozay19/5d86e72ee85b90dca0fa2a103a4efe49 to your computer and use it in GitHub Desktop.
Save ghozay19/5d86e72ee85b90dca0fa2a103a4efe49 to your computer and use it in GitHub Desktop.
service.dart
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:movie_db/core/model/movie_resp.dart';
class Service {
String nowPlayingUrl =
'https://api.themoviedb.org/3/movie/now_playing?api_key=MASUKANAPIKEYKAMUDISINI&language=en-US&page=1&region=ID';
String upComingUrl =
'https://api.themoviedb.org/3/movie/upcoming?api_key=MASUKANAPIKEYKAMUDISINI&language=en-US&page=1';
Future<MovieResp> getUpcoming() async {
try {
print('Request : $upComingUrl');
///hit api
final response = await http.get(upComingUrl);
if (response.statusCode == 200) {
/// jika response status code 200 atw berhasil maka json akan dimapping dan di decode
Map json = jsonDecode(response.body);
print('responsenya : ' + json.toString());
MovieResp movieResp = MovieResp.fromJson(json);
return movieResp;
} else {
///jika gagal
print('failed');
}
} catch (e) {
print(e.toString());
}
}
Future<MovieResp> getNowPlaying() async {
try {
print('Request : $upComingUrl');
final response = await http.get(upComingUrl);
if (response.statusCode == 200) {
Map json = jsonDecode(response.body);
print('responsenya : ' + json.toString());
MovieResp movieResp = MovieResp.fromJson(json);
return movieResp;
} else {
print('failed');
}
} catch (e) {
print(e.toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment