Advertisement
yudiwibisono

flutter_put

May 27th, 2023
1,038
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.39 KB | None | 0 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; //201 artinya berhasil
  14.   String url = "http://127.0.0.1:8000/update_mhs_put/";
  15.  
  16.   Future<int> fetchData() async {
  17.     //data disimpan di body
  18.     String nim = "13594022";
  19.     //nim tambahkan di url
  20.     //pastikan http.put! bukan post
  21.     final response = await http.put(Uri.parse(url + nim),
  22.         headers: <String, String>{
  23.           'Content-Type': 'application/json; charset=UTF-8'
  24.         },
  25.         body: """
  26.      {
  27.      "nim":"13594022",  
  28.      "nama": "Ahmad Aulia2",
  29.      "id_prov": "142",
  30.      "angkatan": "2022",
  31.      "tinggi_badan": 192} """);
  32.     return response.statusCode; //sukses kalau 200
  33.   }
  34.  
  35.   @override
  36.   void initState() {
  37.     super.initState();
  38.     respPost = Future.value(0); //init
  39.   }
  40.  
  41.   @override
  42.   Widget build(BuildContext context) {
  43.     return MaterialApp(
  44.       title: 'My App',
  45.       home: Scaffold(
  46.         appBar: AppBar(
  47.           title: const Text('My App'),
  48.         ),
  49.         body: Center(
  50.             child: Column(
  51.           mainAxisSize: MainAxisSize.min,
  52.           children: [
  53.             ElevatedButton(
  54.               onPressed: () {
  55.                 setState(() {
  56.                   respPost = fetchData();
  57.                 });
  58.               },
  59.               child: const Text('Klik untuk update data (PUT)'),
  60.             ),
  61.             Text("Hasil:"),
  62.             FutureBuilder<int>(
  63.                 future: respPost,
  64.                 builder: (context, snapshot) {
  65.                   if (snapshot.hasData) {
  66.                     if (snapshot.data! == 200) {
  67.                       return Text("Proses Update Berhasil!");
  68.                     }
  69.                     if (snapshot.data! == 0) {
  70.                       return Text("");
  71.                     } else {
  72.                       return Text("Proses insert gagal");
  73.                     }
  74.                   }
  75.                   // default: loading spinner.
  76.                   return const CircularProgressIndicator();
  77.                 })
  78.           ],
  79.         )), //column center
  80.       ), //Scaffold
  81.     ); //Material APP
  82.   }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement