Skip to content

Instantly share code, notes, and snippets.

@ayoubzulfiqar
Created February 11, 2022 18:24
Show Gist options
  • Save ayoubzulfiqar/04684fdb32ba9d5a232de0826721e1d5 to your computer and use it in GitHub Desktop.
Save ayoubzulfiqar/04684fdb32ba9d5a232de0826721e1d5 to your computer and use it in GitHub Desktop.
These gist contain all the mehods to http request
// Model for Json DATA
class Posts {
Posts({
required this.userId,
required this.id,
required this.title,
required this.body,
});
late final int userId;
late final int id;
late final String title;
late final String body;
Posts.fromJson(Map<String, dynamic> json) {
userId = json['userId'] as int;
id = json['id'] as int;
title = json['title'];
body = json['body'];
}
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['userId'] = userId.toString();
data['id'] = id.toString();
data['title'] = title;
data['body'] = body;
return data;
}
}
// HTTP REQUESTs Methods --- PUT POST DELETE PATCH FETCH
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
// fetching the the data from api
Future<List<Posts>> getPosts(http.Client client) async {
final response =
await client.get(Uri.parse('https://jsonplaceholder.typicode.com/posts'));
return compute(parsePosts, response.body);
}
List<Posts> parsePosts(responseBody) {
final parsed = jsonDecode(responseBody);
return parsed.map<Posts>((json) => Posts.fromJson(json)).toList();
}
// deleting the stuff inside the api
Future<void> deletePost(int id) async {
final response = await http.delete(
Uri.parse("https://jsonplaceholder.typicode.com/posts/$id"),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
);
if (response.statusCode == 200) {
print('Deleted');
} else {
throw Exception('Failed to delete');
}
}
Future<void> postPost(Posts posts) async {
print('${posts.toJson()}');
var url = Uri.parse("https://jsonplaceholder.typicode.com/posts//posts/");
var result = '';
var response = await http.put(url, body: posts.toJson());
print(response.body);
print(response.statusCode);
}
// put and post are almost looks same because some backend have put method or postbasically same almost i think
Future<void> putPosts(Posts posts) async {
print('${posts.toJson()}');
var url = Uri.parse("https://jsonplaceholder.typicode.com/posts//posts/");
var result = '';
var response = await http.put(url, body: posts.toJson());
print(response.body);
print(response.statusCode);
}
// to patch the stuff that already exists
Future<void> patchPosts(Posts posts) async {
var url = Uri.parse('https://jsonplaceholder.typicode.com/posts$posts.id');
// String responseData = '';
await http.patch(url, body: {
// body is the stuff that in json data that you wanna change
"id": (posts.id).toString(),
'title': (posts.title).toString(),
}, headers: {
'AuthrizationKey': 'APIToken'
}).then((response) {
Map<String, dynamic> result = jsonDecode(response.body);
print(result);
// return responseData = result[''];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment