Slide 1

Slide 1 text

Buat Aplikasi dengan Mengambil Data dari API Dicoding Developer Coaching #57 Achmad Ilham Curriculum Developer Dicoding

Slide 2

Slide 2 text

Mengapa kode asinkron? ● Memuat data dari internet. ● Menulis data ke database. ● Membuka file dari perangkat.

Slide 3

Slide 3 text

Mengapa kode asinkron?

Slide 4

Slide 4 text

Skema pemanggilan data ke internet

Slide 5

Slide 5 text

Future ● Objek yang mewakili hasil operasi asinkron. ● Memiliki 3 keluaran : ○ Uncompleted ○ Completed with data ○ Completed with error

Slide 6

Slide 6 text

Future void main() async { final uncompleted = futureFunction1(); print("uncompleted: $uncompleted"); final completedWithData = await futureFunction1(); print("completedWithData: $completedWithData"); final completedWithError = await futureFunction2(); print("completedWithError: $completedWithError"); } Future futureFunction1() async { return await Future.delayed( const Duration(seconds: 1), () { return true; }, ); } Future futureFunction2() async { return await Future.delayed( const Duration(seconds: 1), () { throw "Terjadi kesalahan"; }, ); }

Slide 7

Slide 7 text

Future void main() async { final uncompleted = futureFunction1(); print("uncompleted: $uncompleted"); final completedWithData = await futureFunction1(); print("completedWithData: $completedWithData"); final completedWithError = await futureFunction2(); print("completedWithError: $completedWithError"); } Result : uncompleted: Instance of '_Future' completedWithData: true Uncaught Error: Terjadi kesalahan

Slide 8

Slide 8 text

FutureBuilder Widget Widget yang mengembalikan nilai widget yang berjalan saat proses asinkron berlangsung.

Slide 9

Slide 9 text

Pengecekan Data pada Snapshot : ● hasData ⇒ snapshot.data ● hasError ⇒ snapshot.error FutureBuilder Widget ConnectionState pada Snapshot : ● none ● active ● waiting ● done

Slide 10

Slide 10 text

Memuat data melalui internet - http Package ● Library berbasis Future yang dibuat untuk melakukan request HTTP. ● Mendukung seluruh platform, yaitu : ○ Mobile (Android dan iOS), ○ Web ○ Desktop (Windows, MacOS, Linux) https://pub.dev/packages/http

Slide 11

Slide 11 text

Memuat data melalui internet - network request https://docs.flutter.dev/cookbook/networking/fetch-data import 'package:http/http.dart' as http; Future fetchAlbum() { return http.get( Uri.parse( 'https://jsonplaceholder.typicode.com/albums/1', ), ); }

Slide 12

Slide 12 text

Memuat data melalui internet - change response Future fetchAlbum() async { final response = await http .get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1')); if (response.statusCode == 200) { return Album.fromJson(jsonDecode(response.body)); } else { throw Exception('Failed to load album'); } } https://docs.flutter.dev/cookbook/networking/fetch-data

Slide 13

Slide 13 text

Memuat data melalui internet - fetch data class _MyHomePageState extends State { late Future futureFunction; @override void initState() { super.initState(); futureFunction = fetchAlbum(); } ... } https://docs.flutter.dev/cookbook/networking/fetch-data

Slide 14

Slide 14 text

Memuat data melalui internet - display data https://docs.flutter.dev/cookbook/networking/fetch-data FutureBuilder( future: futureFunction, builder: (context, snapshot) { if (snapshot.hasData) { return Text("data : ${snapshot.data!}"); } else if (snapshot.hasError) { return Text("error : ${snapshot.error}"); } return const CircularProgressIndicator(); }, ),

Slide 15

Slide 15 text

Let’s Code

Slide 16

Slide 16 text

Dicoding Dicoding Dicoding Dicoding Indonesia Contact us : Contact me : [email protected]