Advertisement
Sebuahhobi98

controller_mesengger

May 13th, 2023
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.23 KB | Source Code | 0 0
  1. class MesenggerController extends GetxController {
  2.   final _helper = MyHelper();
  3.   var data = <Map<String, dynamic>>[].obs;
  4.   final RxBool loading = true.obs;
  5.   final RxBool finish = false.obs;
  6.  
  7. Future<void> kirimPesan(String text) async {
  8.     String token = await _helper.readStorage('token');
  9.     String userID = await _helper.readStorage('userID');
  10.  
  11.     var url = Uri.parse('http://$ip/mesengger/insert');
  12.     var headers = {'Content-Type': 'application/json'};
  13.  
  14.     var dataJson = {
  15.       'token': token,
  16.     };
  17.     var body = jsonEncode(dataJson);
  18.  
  19.     try {
  20.       var response = await http.post(url, headers: headers, body: body);
  21.       var parsedBody = jsonDecode(response.body);
  22.       if (response.statusCode == 200) {
  23.         Map<String, dynamic> responseMap = parsedBody;
  24.         List<Map<String, dynamic>> dataList = responseMap['data'].cast<Map<String, dynamic>>();
  25.  
  26.         data.assignAll(dataList);
  27.         loading.value = false;
  28.         finish.value = true;
  29.       } else {
  30.         loading.value = false;
  31.         finish.value = false;
  32.         data.clear();
  33.       }
  34.     } catch (error) {
  35.       loading.value = false;
  36.       finish.value = false;
  37.       data.clear();
  38.       log('Error bro: $error');
  39.     }
  40.   }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement