Advertisement
yudiwibisono

flutter_delete

May 27th, 2023
1,187
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.10 KB | None | 1 0
  1. import 'package:flutter/material.dart';
  2. import 'package:http/http.dart' as http;
  3.  
  4. void main() => runApp(MaterialApp(home: MyApp()));
  5.  
  6. class MyApp extends StatefulWidget {
  7.   const MyApp({Key? key}) : super(key: key);
  8.   @override
  9.   MyAppState createState() => MyAppState();
  10. }
  11.  
  12. class MyAppState extends State<MyApp> {
  13.   late Future<int> respPost;
  14.   String url = "http://127.0.0.1:8000/delete_mhs/";
  15.  
  16.   Future<int> fetchData() async {
  17.     //data disimpan di body
  18.     String nim = "13594022";
  19.     //nim tambahkan di url
  20.     //pastikan http.delete
  21.     final response = await http.delete(Uri.parse(url + nim)); //hanya ganti nama saja
  22.     return response.statusCode; //sukses kalau 200
  23.   }
  24.  
  25.   @override
  26.   void initState() {
  27.     super.initState();
  28.     respPost = Future.value(0); //init
  29.   }
  30.  
  31.   @override
  32.   Widget build(BuildContext context) {
  33.     return MaterialApp(
  34.       title: 'My App',
  35.       home: Scaffold(
  36.         appBar: AppBar(
  37.           title: const Text('My App'),
  38.         ),
  39.         body: Center(
  40.             child: Column(
  41.           mainAxisSize: MainAxisSize.min,
  42.           children: [
  43.             ElevatedButton(
  44.               onPressed: () {
  45.                 setState(() {
  46.                   respPost = fetchData();
  47.                 });
  48.               },
  49.               child: const Text('Klik untuk delete data'),
  50.             ),
  51.             Text("Hasil:"),
  52.             FutureBuilder<int>(
  53.                 future: respPost,
  54.                 builder: (context, snapshot) {
  55.                   if (snapshot.hasData) {
  56.                     if (snapshot.data! == 200) {
  57.                       return Text("Proses delete berhasil!");
  58.                     }
  59.                     if (snapshot.data! == 0) {
  60.                       return Text("");
  61.                     } else {
  62.                       return Text("Proses delete gagal");
  63.                     }
  64.                   }
  65.                   // default: loading spinner.
  66.                   return const CircularProgressIndicator();
  67.                 })
  68.           ],
  69.         )), //column center
  70.       ), //Scaffold
  71.     ); //Material APP
  72.   }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement