yudiwbs

flutter_api_1

May 22nd, 2022
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.30 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:http/http.dart' as http;
  3. import 'dart:convert';
  4.  
  5. // sebaiknya di class terpisah
  6. // menapung data hasil pemanggilan API
  7. class CatFact {
  8.  
  9.   String fakta;
  10.   int panjang;
  11.  
  12.   CatFact({required this.fakta, required this.panjang});
  13.  
  14.   //map dari json ke atribut
  15.   factory CatFact.fromJson(Map<String, dynamic> json) {
  16.     return CatFact(
  17.       fakta: json['fact'],
  18.       panjang: json['length'],
  19.     );
  20.   }
  21. }
  22.  
  23. void main() {
  24.   runApp(MyApp());
  25. }
  26.  
  27. class MyApp extends StatefulWidget {
  28.   @override
  29.   State<StatefulWidget> createState() {
  30.     return MyAppState();
  31.   }
  32. }
  33.  
  34. //class state
  35. class MyAppState extends State<MyApp> {
  36.   late Future<CatFact> futureCatFact;
  37.   String url = "https://catfact.ninja/fact";
  38.  
  39.  
  40.   //fetch data
  41.   Future<CatFact> fetchData() async {
  42.     final response = await http.get(Uri.parse(url));
  43.  
  44.     if (response.statusCode == 200) {
  45.       // jika server mengembalikan 200 OK (berhasil),
  46.       // parse json
  47.       return CatFact.fromJson(jsonDecode(response.body));
  48.     } else {
  49.       // jika gagal (bukan  200 OK),
  50.       // lempar exception
  51.       throw Exception('Gagal load');
  52.     }
  53.   }
  54.  
  55.   @override
  56.   void initState() {
  57.     super.initState();
  58.     futureCatFact = fetchData();
  59.   }
  60.  
  61.   @override
  62.   Widget build(BuildContext context) {
  63.     return MaterialApp(
  64.         title: 'coba http',
  65.         home: Scaffold(
  66.           appBar: AppBar(
  67.             title: const Text('coba http'),
  68.           ),
  69.           body: Center(
  70.             child: FutureBuilder<CatFact>(
  71.               future: futureCatFact,
  72.               builder: (context, snapshot) {
  73.                 if (snapshot.hasData) {
  74.                   return Center(
  75.                       child: Column(
  76.                           mainAxisAlignment: MainAxisAlignment.center,
  77.                           children: [
  78.                         Text(snapshot.data!.fakta),
  79.                         Text(snapshot.data!.panjang.toString())
  80.                       ]));
  81.                 } else if (snapshot.hasError) {
  82.                   return Text('${snapshot.error}');
  83.                 }
  84.                 // By default, show a loading spinner.
  85.                 return const CircularProgressIndicator();
  86.               },
  87.             ),
  88.           ),
  89.         ));
  90.   }
  91. }
  92.  
Add Comment
Please, Sign In to add comment