File tree 5 files changed +54
-0
lines changed
5 files changed +54
-0
lines changed Original file line number Diff line number Diff line change 1
1
<manifest xmlns : android =" http://schemas.android.com/apk/res/android"
2
2
package =" com.example.simple_http" >
3
+ <!-- Required to fetch data from the internet. -->
4
+ <uses-permission android : name =" android.permission.INTERNET" />
3
5
<application
4
6
android : label =" simple_http"
5
7
android : icon =" @mipmap/ic_launcher" >
Original file line number Diff line number Diff line change
1
+ class Album {
2
+ final int userId;
3
+ final int id;
4
+ final String title;
5
+
6
+ Album ({required this .userId, required this .id, required this .title});
7
+
8
+ factory Album .fromJson (Map <String , dynamic > json) {
9
+ return Album (userId: json['userId' ], id: json['id' ], title: json['title' ]);
10
+ }
11
+ }
Original file line number Diff line number Diff line change
1
+ import 'dart:convert' ;
2
+
3
+ import 'package:simple_http/data/model/album.dart' ;
4
+ import 'package:http/http.dart' as http;
5
+
6
+ class RemoteRepository {
7
+ Future <List <Album >> fetchAlbumAPI () async {
8
+ final baseUrl = "https://jsonplaceholder.typicode.com/albums" ;
9
+ final response = await http.get (Uri .parse (baseUrl));
10
+ if (response.statusCode == 200 ) {
11
+ print (response.body);
12
+ Iterable it = jsonDecode (response.body);
13
+ List <Album > listResult = it.map ((e) => Album .fromJson (e)).toList ();
14
+ return listResult;
15
+ } else {
16
+ throw Exception ('Error fetching data album' );
17
+ }
18
+ }
19
+ }
Original file line number Diff line number Diff line change @@ -67,6 +67,20 @@ packages:
67
67
description: flutter
68
68
source: sdk
69
69
version: "0.0.0"
70
+ http:
71
+ dependency: "direct main"
72
+ description:
73
+ name: http
74
+ url: "https://pub.dartlang.org"
75
+ source: hosted
76
+ version: "0.13.3"
77
+ http_parser:
78
+ dependency: transitive
79
+ description:
80
+ name: http_parser
81
+ url: "https://pub.dartlang.org"
82
+ source: hosted
83
+ version: "4.0.0"
70
84
matcher:
71
85
dependency: transitive
72
86
description:
@@ -88,6 +102,13 @@ packages:
88
102
url: "https://pub.dartlang.org"
89
103
source: hosted
90
104
version: "1.8.0"
105
+ pedantic:
106
+ dependency: transitive
107
+ description:
108
+ name: pedantic
109
+ url: "https://pub.dartlang.org"
110
+ source: hosted
111
+ version: "1.11.1"
91
112
sky_engine:
92
113
dependency: transitive
93
114
description: flutter
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ dependencies:
28
28
# The following adds the Cupertino Icons font to your application.
29
29
# Use with the CupertinoIcons class for iOS style icons.
30
30
cupertino_icons : ^1.0.2
31
+ http : ^0.13.3
31
32
32
33
dev_dependencies :
33
34
flutter_test :
You can’t perform that action at this time.
0 commit comments