Created
February 16, 2020 06:47
-
-
Save ghozay19/5d86e72ee85b90dca0fa2a103a4efe49 to your computer and use it in GitHub Desktop.
service.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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®ion=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