Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:convert';
- import 'package:flutter/material.dart';
- import 'package:flutter/widgets.dart';
- import 'package:http/http.dart' as http;
- import 'package:project-name/Models/model.dart'; // Change project-name to you project name
- class HttpService {
- final String url;
- final Map<String, dynamic> body;
- var network;
- HttpService({
- @required this.url,
- this.body,
- });
- /*
- * Send a Post request
- */
- Future<Model> post(context) async {
- try {
- http.Response res = await http.post(
- Uri.encodeFull(url),
- headers: {
- 'Content-Type': 'application/json; charset=UTF-8',
- 'Accept': 'application/json',
- },
- body: jsonEncode(body),
- );
- var response = jsonDecode(res.body);
- return Model.fromJson(response);
- } catch (e) {
- throw e;
- }
- }
- /*
- * Send a Post request
- */
- // ignore: missing_return
- Future<Model> getRequest() async {
- try {
- http.Response res = await http.get(
- Uri.encodeFull(url),
- headers: {
- 'Content-Type': 'application/json; charset=UTF-8',
- 'Accept': 'application/json',
- },
- );
- var response = jsonDecode(res.body);
- return Model.fromJson(response);
- } catch (e) {
- // throw e;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement