-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.dart
62 lines (48 loc) · 1.37 KB
/
http.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import 'dart:convert';
import 'package:http/http.dart' as http;
class HttpService {
static String get baseUrl => "REPLACE_POST_URL";
static Future<List<ExampleModel>> getDataResponse(
{Map<String, dynamic>? body}) async {
http.Response res = await http.post(Uri.parse(baseUrl), body: body);
if (res.statusCode == 200) {
List<dynamic> body = jsonDecode(res.body);
List<ExampleModel> data = body
.map(
(dynamic item) => ExampleModel.fromJson(item),
)
.toList();
return data;
} else {
throw "Can't get data.";
}
}
static Future<List<ExampleModel>> getResponse() async {
http.Response res = await http
.get(Uri.parse(baseUrl), headers: {"Content-Type": "application/json"});
if (res.statusCode == 200) {
List<dynamic> body = jsonDecode(res.body);
List<ExampleModel> users =
body.map((dynamic item) => ExampleModel.fromJson(item)).toList();
return users;
} else {
throw "Can't get data.";
}
}
}
class ExampleModel {
String? message;
int? status;
ExampleModel({
this.message = "",
this.status,
});
ExampleModel.fromJson(Map<String, dynamic> map) {
message = map['message'] ?? "";
status = map['status'] ?? 0;
}
Map<String, dynamic> toJson() => {
'message': message,
'status': status,
};
}