Go straight to code
import 'package:dio/dio.dart';
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart'; // Use the local cache policy to obtain the token
class Http {
Response resp;
Dio dio = new Dio();
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
String host = '* * * * * * *'; This is where you put your API URL Futureget(String url, {Map query}) async {
final SharedPreferences prefs = await _prefs;
try {
resp = await dio.get(host+url,
options: Options(headers: {"token":prefs.getString('token')/ * access token * /}), queryParameters: query);
if (resp.statusCode == 200) {
var result = resp.data;
var res = jsonDecode(result);
return res;
} else {
print('Request error'); }}catch (e) {
return e;
}
}
Future post(String url, {data}) async {
final SharedPreferences prefs = await _prefs;
try {
resp =
await dio.post(host+url, options: Options(headers: {"token":prefs.getString('token')}), data: data);
if (resp.statusCode == 200) {
var result = resp.data;
var res = jsonDecode(result);
return res;
} else {
print('Request error'); }}catch (e) {
returne; }}}Copy the code