post<T> method
Future<T?>
post<
T>({ - required String url,
- Options? options,
- Object? data,
- dynamic onReceiveProgress(
- int,
- int
)?,
- CancelToken? cancelToken,
- Map<String, dynamic>? queryParameters,
- dynamic onError(
- dynamic
)?,
- T? responseBuilder(
- Response
)?,
- bool withLoading = false,
})
Implementation
Future<T?> post<T>({required final String url,
Map<String,dynamic>? headers,
final Options? options,
Object? data,
final Function(int,int)? onReceiveProgress,
CancelToken? cancelToken,
Map<String, dynamic>? queryParameters,
final Function(dynamic)? onError,
final T? Function(Response)? responseBuilder,
final bool withLoading = false,
})async {
final Dio dio = Dio();
if (queryParameters!=null && queryMapFilter!=null) {
queryParameters = await queryMapFilter!(queryParameters!);
}
if( data != null && dataMapFilter!=null){
data = await dataMapFilter!(data!);
}
headers??=defaultHeaders;
if(kDebugMode){
print(""
"\n sending request to $url with headers $headers");
}
final Response? response = await FunctionHelpers.tryFuture<Response>(dio.post(url,
options: (options?..headers = headers) ?? Options(headers:headers,
validateStatus:(status)=>true
),
queryParameters:queryParameters,
cancelToken:cancelToken,
data:data,
onReceiveProgress:onReceiveProgress,
),
withLoading:withLoading,
onError:onError
);
if(response!=null ){
if(kDebugMode) {
print("$url \n response status is ${response.statusCode} ${response
.statusMessage}");
print("data is ${response.data}");
}
if(responseBuilder!=null)responseBuilder(response);
if( onResponseReady!=null && response is! T) return onResponseReady!(response);
} else {
if(kDebugMode){
print("response is null from url ${url}");
}
}
return response as T?;
}