Advertisement
yocky12k

flutter api

Dec 19th, 2023 (edited)
1,428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.12 KB | None | 0 0
  1. dependencies:
  2.   flutter:
  3.     sdk: flutter
  4.   http: ^0.13.6
  5.   provider: ^6.1.1
  6.  
  7. file api_service.dart
  8.  
  9. import 'package:http/http.dart' as http;
  10. import 'dart:convert';
  11.  
  12. class ApiService {
  13.   Future<List<dataCrypto>> bacaApi() async {
  14.     final response = await http.get(Uri.parse('https://api.coinlore.net/api/tickers/'));
  15.     if(response.statusCode == 200){
  16.       final List<dynamic> dataList = json.decode(response.body)['data'];
  17.       List<dataCrypto> cryptoList = dataList.map((data) => dataCrypto.fromJson(data)).toList();
  18.       return cryptoList;
  19.     }else{
  20.       throw Exception('Gagal baca data');
  21.     }
  22.   }
  23. }
  24.  
  25. class dataCrypto {
  26.   final String id;
  27.   final String symbol;
  28.   final String name;
  29.  
  30.   dataCrypto({
  31.     required this.id,
  32.     required this.symbol,
  33.     required this.name,
  34.   });
  35.  
  36.   factory dataCrypto.fromJson(Map<String, dynamic> json){
  37.     return dataCrypto(
  38.       id: json['id'],
  39.       symbol: json['symbol'],
  40.       name: json['name'],
  41.       );
  42.   }
  43. }
  44.  
  45. file provider_crypto.dart
  46.  
  47. import 'package:flutter/material.dart';
  48. import 'api_service.dart';
  49.  
  50. class ProviderCrypto extends ChangeNotifier {
  51.   final ApiService apiService = ApiService();
  52.   List<dataCrypto> cryptoList = [];
  53.  
  54.   Future<void> ambilApi() async {
  55.     try {
  56.        cryptoList = await apiService.bacaApi();
  57.        notifyListeners();
  58.     } catch (e) {
  59.        print('Error: $e');
  60.     }
  61.   }
  62. }
  63.  
  64. file main.dart
  65. import 'package:flutter/material.dart';
  66. import 'package:provider/provider.dart';
  67. import 'provider_crypto.dart';
  68.  
  69. void main() {
  70.   runApp(
  71.     ChangeNotifierProvider(
  72.       create: (context) => ProviderCrypto(),
  73.       child: MyApp(),
  74.     ),
  75.   );
  76. }
  77.  
  78. class MyApp extends StatelessWidget {
  79.   @override
  80.   Widget build(BuildContext context) {
  81.     return MaterialApp(
  82.       home: MyHomePage(),
  83.     );
  84.   }
  85. }
  86.  
  87. class MyHomePage extends StatelessWidget {
  88.   @override
  89.   Widget build(BuildContext context) {
  90.     final cryptoProvider = Provider.of<ProviderCrypto>(context);
  91.  
  92.     return Scaffold(
  93.       appBar: AppBar(
  94.         title: Text('Crypto List'),
  95.       ),
  96.       body: Column(
  97.         mainAxisAlignment: MainAxisAlignment.center,
  98.         children: [
  99.           SizedBox(height: 10),
  100.           ElevatedButton(
  101.             onPressed: () {
  102.               cryptoProvider.ambilApi();
  103.             },
  104.             child: Text('Ambil Data'),
  105.           ),
  106.           SizedBox(height: 20),
  107.           Expanded(
  108.             child: ListView.builder(
  109.               itemCount: cryptoProvider.cryptoList.length,
  110.               itemBuilder: (context, index) {
  111.                 final crypto = cryptoProvider.cryptoList[index];
  112.                 return Card(
  113.                   child: ListTile(
  114.                     leading: Text(
  115.                       crypto.symbol,
  116.                       style: TextStyle(
  117.                         color: Colors.blue,
  118.                         fontSize: 15,
  119.                       ),
  120.                     ),
  121.                     title: Text(crypto.name+' ('+crypto.id+')'),
  122.                   ),
  123.                 );
  124.               },
  125.             ),
  126.           ),
  127.         ],
  128.       ),
  129.     );
  130.   }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement