暫無描述

Services.dart 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import 'dart:convert';
  2. import 'package:http/http.dart'
  3. as http;
  4. import 'Oficina.dart';
  5. class Services {
  6. static const URL = 'https://ada.uprrp.edu/~oniel.mendez2/json/oficinas.php';
  7. static const URL_GET_ALL = 'ada.uprrp.edu/~oniel.mendez2/json/getOficinas.php';
  8. static const URL_LOCAL = 'http://localhost/test/getOficinas.json';
  9. static const _GET_ALL = "GET_ALL";
  10. //get all oficinas
  11. static Future<List<Oficina>> getOficinas() async {
  12. try {
  13. var map = Map<String, dynamic>();
  14. map['action'] = _GET_ALL;
  15. //print('antes');
  16. final response = await http.get(URL_GET_ALL);
  17. // print('despues');
  18. // print('getOficinas Response: ${response.body}');
  19. if (200 == response.statusCode) {
  20. List<Oficina> list = parseResponse(response.body);
  21. return list;
  22. } else {
  23. final response = '[{"id":"1","name":"no"},{"id":"2","name":"respondio"},{"id":"3","name":"code: 200"}]';
  24. List<Oficina> list = parseResponse(response);
  25. return list; // return an empty list on exception/error
  26. }
  27. } catch (e) {
  28. final response = '[{"id":"1","name":"este"},{"id":"2","name":"es el"},{"id":"3","name":"catch(e)"}]';
  29. List<Oficina> list = parseResponse(response);
  30. return list; // return an empty list on exception/error
  31. }
  32. }
  33. static List<Oficina> parseResponse(String responseBody) {
  34. final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();
  35. return parsed.map<Oficina>((json) => Oficina.fromJson(json)).toList();
  36. }
  37. Future<http.Response> fetchAlbum() {
  38. return http.get('https://jsonplaceholder.typicode.com/albums/1');
  39. }
  40. }