forked from SaadOcloud/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoke_api_calling.dart
More file actions
219 lines (192 loc) · 5.49 KB
/
joke_api_calling.dart
File metadata and controls
219 lines (192 loc) · 5.49 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// This is flutter's http API calling method. I've also included the dependencies that I used
import 'package:api_get/home.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const Home(),
);
}
}
class Home extends StatefulWidget {
const Home({super.key});
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
JokeApi client = JokeApi();
@override
Widget build(BuildContext context) {
final width = MediaQuery.of(context).size.width;
final height = MediaQuery.of(context).size.height;
TextEditingController keyword = TextEditingController();
return Scaffold(
body: FutureBuilder<Jokemodel?>(
future: client.getJoke(keyword.text),
builder: (context, snapshot) {
var response = snapshot.data;
return Container(
height: height / 2,
width: width,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: TextFormField(
controller: keyword,
validator: (value) {
if (keyword == "") {
return "Please Enter a Keyword";
}
},
decoration: const InputDecoration(
labelText: "Enter Key",
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.blue,
))),
),
),
ElevatedButton(
onPressed: () {
JokeApi().getJoke(keyword.text);
},
child: const Text("search")),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Text(
'${response?.setup}',
style: TextStyle(fontSize: 24, color: Colors.black),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Text(
'${response?.delivery}',
style: TextStyle(fontSize: 24, color: Colors.black),
),
),
],
),
);
},
),
);
}
}
// ================== Api Services ==================
class JokeApi {
Future<Jokemodel?> getJoke(String keyword) async {
var url = Uri.parse("https://v2.jokeapi.dev/joke/Any?$keyword");
try {
var response = await http.get(url);
if (response.statusCode == 200) {
print(response.body);
return Jokemodel.fromJson(jsonDecode(response.body));
}
print(response.body.toString());
} catch (e) {
print(e.toString());
}
}
}
// ======================= API Model==================
import 'dart:convert';
Jokemodel jokemodelFromJson(String str) => Jokemodel.fromJson(json.decode(str));
String jokemodelToJson(Jokemodel data) => json.encode(data.toJson());
class Jokemodel {
Jokemodel({
this.error,
this.category,
this.type,
this.setup,
this.delivery,
this.flags,
this.id,
this.safe,
this.lang,
});
bool? error;
String? category;
String? type;
String? setup;
String? delivery;
Flags? flags;
int? id;
bool? safe;
String? lang;
factory Jokemodel.fromJson(Map<String, dynamic> json) => Jokemodel(
error: json["error"],
category: json["category"],
type: json["type"],
setup: json["setup"],
delivery: json["delivery"],
flags: Flags.fromJson(json["flags"]),
id: json["id"],
safe: json["safe"],
lang: json["lang"],
);
Map<String, dynamic> toJson() => {
"error": error,
"category": category,
"type": type,
"setup": setup,
"delivery": delivery,
"flags": flags!.toJson(),
"id": id,
"safe": safe,
"lang": lang,
};
}
class Flags {
Flags({
this.nsfw,
this.religious,
this.political,
this.racist,
this.sexist,
this.explicit,
});
bool? nsfw;
bool? religious;
bool? political;
bool? racist;
bool? sexist;
bool? explicit;
factory Flags.fromJson(Map<String, dynamic> json) => Flags(
nsfw: json["nsfw"],
religious: json["religious"],
political: json["political"],
racist: json["racist"],
sexist: json["sexist"],
explicit: json["explicit"],
);
Map<String, dynamic> toJson() => {
"nsfw": nsfw,
"religious": religious,
"political": political,
"racist": racist,
"sexist": sexist,
"explicit": explicit,
};
}
/*
=============== dependencies ===================
http: ^0.13.5
get: ^4.6.5
*/