No Description

album.dart 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:http/http.dart' as http;
  7. Future<Oficina> fetchOficina() async {
  8. final response =
  9. await http.get('https://ada.uprrp.edu/~oniel.mendez2/json/getOficinas.php');
  10. if (response.statusCode == 200) {
  11. return Oficina.fromJson(jsonDecode(response.body));
  12. } else {
  13. throw Exception('Failed to load album');
  14. }
  15. }
  16. class Oficina {
  17. String id;
  18. String name;
  19. Oficina({this.id, this.name});
  20. factory Oficina.fromJson(Map<String, dynamic> json) {
  21. return Oficina (
  22. id :json['id'],
  23. name: json['name'],
  24. );
  25. }
  26. }
  27. Future<void> main() async {
  28. runApp(album());
  29. }
  30. class album extends StatefulWidget {
  31. album({Key key}) : super(key: key);
  32. @override
  33. _MyAppState2 createState() => _MyAppState2();
  34. }
  35. class _MyAppState2 extends State<album> {
  36. Future<Oficina> futureOficina;
  37. @override
  38. void initState() {
  39. super.initState();
  40. futureOficina = fetchOficina();
  41. }
  42. @override
  43. Widget build(BuildContext context) {
  44. return MaterialApp(
  45. title: 'Fetch Data Example',
  46. theme: ThemeData(
  47. primarySwatch: Colors.blue,
  48. ),
  49. home: Scaffold(
  50. appBar: AppBar(
  51. title: Text('Fetch Data Example'),
  52. ),
  53. body: Center(
  54. child: FutureBuilder<Oficina>(
  55. future: futureOficina,
  56. builder: (context, snapshot) {
  57. if (snapshot.hasData) {
  58. return Text(snapshot.data.name);
  59. } else if (snapshot.hasError) {
  60. return Text("${snapshot.error}");
  61. }
  62. // By default, show a loading spinner.
  63. return CircularProgressIndicator();
  64. },
  65. ),
  66. ),
  67. ),
  68. );
  69. }
  70. }