Skip to content

Commit b2a1fe5

Browse files
author
rivaldy
committed
⚡ add model, and repository
1 parent 316fd41 commit b2a1fe5

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

android/app/src/main/AndroidManifest.xml

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="com.example.simple_http">
3+
<!-- Required to fetch data from the internet. -->
4+
<uses-permission android:name="android.permission.INTERNET" />
35
<application
46
android:label="simple_http"
57
android:icon="@mipmap/ic_launcher">

lib/data/model/album.dart

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}

pubspec.lock

+21
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ packages:
6767
description: flutter
6868
source: sdk
6969
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"
7084
matcher:
7185
dependency: transitive
7286
description:
@@ -88,6 +102,13 @@ packages:
88102
url: "https://pub.dartlang.org"
89103
source: hosted
90104
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"
91112
sky_engine:
92113
dependency: transitive
93114
description: flutter

pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dependencies:
2828
# The following adds the Cupertino Icons font to your application.
2929
# Use with the CupertinoIcons class for iOS style icons.
3030
cupertino_icons: ^1.0.2
31+
http: ^0.13.3
3132

3233
dev_dependencies:
3334
flutter_test:

0 commit comments

Comments
 (0)