Advertisement
yudiwbs

Untitled

Apr 9th, 2023
1,408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.99 KB | None | 0 0
  1. import 'package:flutter_bloc/flutter_bloc.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:http/http.dart' as http;
  4. import 'dart:convert';
  5.  
  6. //event parent
  7. abstract class DataEvent {}
  8.  
  9. //event mulai pengambilan data
  10. class FetchDataEvent extends DataEvent {}
  11.  
  12. //event jika data sudah selesai diambil
  13. class DataSiapEvent extends DataEvent {
  14.   late ActivityModel activity;
  15.   DataSiapEvent(ActivityModel act) : activity = act;
  16. }
  17.  
  18. class ActivityBloc extends Bloc<DataEvent, ActivityModel> {
  19.   String url = "https://www.boredapi.com/api/activity";
  20.   ActivityBloc() : super(ActivityModel(aktivitas: "", jenis: "")) {
  21.    
  22.     //penanganan event
  23.     on<FetchDataEvent>((event, emit) {
  24.       fetchData(); //request ambi ldata
  25.     });
  26.     on<DataSiapEvent>((even, emit) {
  27.       emit(even.activity);  //selesai, emit state data terakhir
  28.     });
  29.   }
  30.  
  31.   //map dari json ke atribut
  32.   void setFromJson(Map<String, dynamic> json) {
  33.     String aktivitas = json['activity'];
  34.     String jenis = json['type'];
  35.     //tambahkan event bahwa data sudah difetch dan siap
  36.     add(DataSiapEvent(ActivityModel(aktivitas: aktivitas, jenis: jenis)));
  37.   }
  38.  
  39.   void fetchData() async {
  40.     final response = await http.get(Uri.parse(url));
  41.     if (response.statusCode == 200) {
  42.       setFromJson(jsonDecode(response.body));
  43.     } else {
  44.       throw Exception('Gagal load');
  45.     }
  46.   }
  47. }
  48.  
  49. class ActivityModel {
  50.   String aktivitas;
  51.   String jenis;
  52.   ActivityModel({required this.aktivitas, required this.jenis}); //constructor
  53. }
  54.  
  55. void main() => runApp(const MyApp());
  56.  
  57. class MyApp extends StatelessWidget {
  58.   const MyApp({Key? key}) : super(key: key);
  59.  
  60.   @override
  61.   Widget build(BuildContext context) {
  62.     return MaterialApp(
  63.       home: BlocProvider(
  64.         create: (_) => ActivityBloc(),
  65.         child: const HalamanUtama(),
  66.       ),
  67.     );
  68.   }
  69. }
  70.  
  71. class HalamanUtama extends StatelessWidget {
  72.   const HalamanUtama({Key? key}) : super(key: key);
  73.   @override
  74.   Widget build(Object context) {
  75.     return MaterialApp(
  76.         home: Scaffold(
  77.       body: Center(
  78.         child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
  79.           BlocBuilder<ActivityBloc, ActivityModel>(
  80.             builder: (context, aktivitas) {
  81.               return Center(
  82.                   child: Column(
  83.                       mainAxisAlignment: MainAxisAlignment.center,
  84.                       children: [
  85.                     Padding(
  86.                       padding: const EdgeInsets.only(bottom: 20),
  87.                       child: ElevatedButton(
  88.                         onPressed: () {
  89.                           context.read<ActivityBloc>().add(FetchDataEvent());
  90.                         },
  91.                         child: const Text("Saya bosan ..."),
  92.                       ),
  93.                     ),
  94.                     Text(aktivitas.aktivitas),
  95.                     Text("Jenis: ${aktivitas.jenis}")
  96.                   ]));
  97.             },
  98.           ),
  99.         ]),
  100.       ),
  101.     ));
  102.   }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement