12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import 'dart:async';
- import 'dart:convert';
- import 'dart:io';
-
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:http/http.dart' as http;
-
- Future<Oficina> fetchOficina() async {
- final response =
- await http.get('https://ada.uprrp.edu/~oniel.mendez2/json/getOficinas.php');
-
- if (response.statusCode == 200) {
- return Oficina.fromJson(jsonDecode(response.body));
- } else {
- throw Exception('Failed to load album');
- }
- }
-
- class Oficina {
- String id;
- String name;
-
- Oficina({this.id, this.name});
-
- factory Oficina.fromJson(Map<String, dynamic> json) {
- return Oficina (
- id :json['id'],
- name: json['name'],
- );
- }
- }
-
-
- Future<void> main() async {
- runApp(album());
- }
- class album extends StatefulWidget {
-
- album({Key key}) : super(key: key);
-
- @override
- _MyAppState2 createState() => _MyAppState2();
- }
-
-
- class _MyAppState2 extends State<album> {
- Future<Oficina> futureOficina;
-
- @override
- void initState() {
- super.initState();
- futureOficina = fetchOficina();
- }
-
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Fetch Data Example',
- theme: ThemeData(
- primarySwatch: Colors.blue,
- ),
- home: Scaffold(
- appBar: AppBar(
- title: Text('Fetch Data Example'),
- ),
- body: Center(
- child: FutureBuilder<Oficina>(
- future: futureOficina,
- builder: (context, snapshot) {
- if (snapshot.hasData) {
- return Text(snapshot.data.name);
- } else if (snapshot.hasError) {
- return Text("${snapshot.error}");
- }
-
- // By default, show a loading spinner.
- return CircularProgressIndicator();
- },
- ),
- ),
- ),
- );
- }
- }
|