Files
daily_mobileapp_merchant/lib/Helper/customDio.dart
2026-05-27 10:35:09 +05:30

170 lines
4.3 KiB
Dart

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:dio/adapter.dart';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class CustomDio{
Dio? _dio;
CustomDio(){
BaseOptions options = BaseOptions(connectTimeout: 100000,receiveTimeout: 100000);
// options.baseUrl = hostApi;
_dio = Dio(options);
// _dio.interceptors.add(LogInterceptor());
}
String? token;
String? stripeDevKey;
getToken()async{
SharedPreferences prefs = await SharedPreferences.getInstance();
token= prefs.getString('token')!;
stripeDevKey= prefs.getString('stripedevkey')!;
print("devkey====$stripeDevKey");
}
Future<Response?> get({@required String? url}) async {
await getToken();
(_dio?.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
(HttpClient client) {
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
return client;
};
var data = {'token':'$token'};
print("wrl$url");
Response? response;
try{
response = await _dio?.get(url!,options: Options(
responseType: ResponseType.plain,
headers: data,contentType: "application/json"
));
}
catch(e){
print(e.toString());
}
return response;
}
Future<Response?> post({@required String? url,
@required dynamic params,Map<String,String>? header
}) async {
// await getToken();
(_dio?.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
(HttpClient client) {
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
return client;
};
Response? response;
try{
response = await _dio!.post(url!,data: params!,options
:Options(responseType: ResponseType.json,contentType: "application/json",headers: header));
}
on DioError catch (e) {
print('dioerror fetch');
if (e.type == DioErrorType.response) {
print('catched');
}
if (e.type == DioErrorType.connectTimeout) {
print('check your connection');
}
if (e.type == DioErrorType.receiveTimeout) {
print('unable to connect to the server');
}
if (e.type == DioErrorType.other) {
print('Something went wrong');
}
print('try errors');
print('errorsssssssssssssssss.....');
print(e.response!);
print(e.response!.data);
print(e.message);
print(e);
}
return response;
}
//verify
Future<Response?> verifyPost({@required String? url,
@required Map<String,dynamic>? params, Map<String,String>? header
}) async {
(_dio?.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
(HttpClient client) {
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
return client;
};
print("data${params.toString()}");
Response? response;
try{
response = await _dio!.post(url!,data: params,options
:Options(responseType: ResponseType.json,
headers:header,
contentType: "application/json"));
}
catch(e){
print(e.toString());
}
print("ressscatcherror${response.toString()}");
return response;
}
//app config
Future<Response> newGet({@required String? url}) async {
(_dio?.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
(HttpClient client) {
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
return client;
};
Response? response;
try{
response = await _dio?.get(url!,options
:Options(responseType: ResponseType.json));
}
catch(e){
print(e.toString());
}
return response!;
}
Future<Response> put({@required String? url,@required Map<String,String>? params,@required Map<String,String>? header}) async {
await getToken();
(_dio?.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
(HttpClient client) {
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
return client;
};
var data = {'x-access-token': '$token'};
Response? response;
try{
response = await _dio!.put(url!,data:params,options: Options(responseType: ResponseType.json,headers: data,contentType: "application/json"));
}
catch(e){
print(e.toString());
}
return response!;
}
}