Skip to content

Commit ed16112

Browse files
author
Avidán García Castillo
committed
Ejemplos peticiones HTTP
1 parent a4c2af0 commit ed16112

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

constructor_factory.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
main(List<String> arg) {
2+
var datos = {'nombre': 'Avidan', 'apellido': 'Garcia Castillo'};
3+
4+
Persona persona = new Persona.asignarDatos(datos);
5+
//print('${persona.nombre} ${persona.apellido}');
6+
print(persona.nombreCompleto());
7+
}
8+
9+
class Persona {
10+
String nombre;
11+
String apellido;
12+
13+
Persona({required this.nombre, required this.apellido});
14+
15+
factory Persona.asignarDatos(Map jsonMap) {
16+
return Persona(nombre: jsonMap['nombre'], apellido: jsonMap['apellido']);
17+
}
18+
19+
//method toString
20+
String nombreCompleto() => "$nombre $apellido";
21+
}

requestHTTP.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'dart:async';
2+
import 'dart:convert';
3+
import 'dart:io';
4+
5+
import 'package:http/http.dart' as http;
6+
7+
8+
9+
10+
Future<Album> fetchAlbum() async {
11+
final response = await http.get(
12+
Uri.parse('https://jsonplaceholder.typicode.com/albums/1'),
13+
// Send authorization headers to the backend.
14+
headers: {
15+
HttpHeaders.authorizationHeader: 'Basic your_api_token_here',
16+
},
17+
);
18+
final responseJson = jsonDecode(response.body);
19+
20+
return Album.fromJson(responseJson);
21+
}
22+
//Data MODEL
23+
class Album {
24+
final int userId;
25+
final int id;
26+
final String title;
27+
28+
Album({
29+
required this.userId,
30+
required this.id,
31+
required this.title,
32+
});
33+
34+
factory Album.fromJson(Map<String, dynamic> json) {
35+
return Album(
36+
userId: json['userId'],
37+
id: json['id'],
38+
title: json['title'],
39+
);
40+
}
41+
}

request_parseJSON.dart

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import 'dart:async';
2+
import 'dart:convert';
3+
4+
import 'package:flutter/foundation.dart';
5+
import 'package:flutter/material.dart';
6+
import 'package:http/http.dart' as http;
7+
8+
Future<List<Photo>> fetchPhotos(http.Client client) async {
9+
final response = await client
10+
.get(Uri.parse('https://jsonplaceholder.typicode.com/photos'));
11+
12+
// Use the compute function to run parsePhotos in a separate isolate.
13+
return compute(parsePhotos, response.body);
14+
}
15+
16+
// A function that converts a response body into a List<Photo>.
17+
List<Photo> parsePhotos(String responseBody) {
18+
final parsed = jsonDecode(responseBody).cast<Map<String, dynamic>>();
19+
20+
return parsed.map<Photo>((json) => Photo.fromJson(json)).toList();
21+
}
22+
23+
class Photo {
24+
final int albumId;
25+
final int id;
26+
final String title;
27+
final String url;
28+
final String thumbnailUrl;
29+
30+
Photo({
31+
required this.albumId,
32+
required this.id,
33+
required this.title,
34+
required this.url,
35+
required this.thumbnailUrl,
36+
});
37+
38+
factory Photo.fromJson(Map<String, dynamic> json) {
39+
return Photo(
40+
albumId: json['albumId'] as int,
41+
id: json['id'] as int,
42+
title: json['title'] as String,
43+
url: json['url'] as String,
44+
thumbnailUrl: json['thumbnailUrl'] as String,
45+
);
46+
}
47+
}
48+
49+
void main() => runApp(MyApp());
50+
51+
class MyApp extends StatelessWidget {
52+
@override
53+
Widget build(BuildContext context) {
54+
final appTitle = 'Isolate Demo';
55+
56+
return MaterialApp(
57+
title: appTitle,
58+
home: MyHomePage(title: appTitle),
59+
);
60+
}
61+
}
62+
63+
class MyHomePage extends StatelessWidget {
64+
final String title;
65+
66+
MyHomePage({Key? key, required this.title}) : super(key: key);
67+
68+
@override
69+
Widget build(BuildContext context) {
70+
return Scaffold(
71+
appBar: AppBar(
72+
title: Text(title),
73+
),
74+
body: FutureBuilder<List<Photo>>(
75+
future: fetchPhotos(http.Client()),
76+
builder: (context, snapshot) {
77+
if (snapshot.hasError) print(snapshot.error);
78+
79+
return snapshot.hasData
80+
? PhotosList(photos: snapshot.data!)
81+
: Center(child: CircularProgressIndicator());
82+
},
83+
),
84+
);
85+
}
86+
}
87+
88+
class PhotosList extends StatelessWidget {
89+
final List<Photo> photos;
90+
91+
PhotosList({Key? key, required this.photos}) : super(key: key);
92+
93+
@override
94+
Widget build(BuildContext context) {
95+
return GridView.builder(
96+
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
97+
crossAxisCount: 2,
98+
),
99+
itemCount: photos.length,
100+
itemBuilder: (context, index) {
101+
return Image.network(photos[index].thumbnailUrl);
102+
},
103+
);
104+
}
105+
}

0 commit comments

Comments
 (0)