imageRequest method
Implementation
Future<Image> imageRequest({
final String? function,
final Map<String, dynamic>? params,
final Map<String, String?>? headers,
final String? url,
//final int timeout = 15000,
final bool debug = false,
final String method = 'GET',
}) async {
final _dio = Dio(
BaseOptions(
headers: headers,
method: method,
responseType: ResponseType.json,
contentType: 'application/json',
connectTimeout: Duration(milliseconds: connectDuration),
receiveTimeout: Duration(milliseconds: requestDuration),
),
);
late Response<Uint8List> response;
try {
if (!kIsWeb) {
(_dio.httpClientAdapter as IOHttpClientAdapter).createHttpClient = () {
final client = HttpClient();
client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
return client;
};
}
if (method == 'GET') {
response = await _dio.get<Uint8List>(
url!,
queryParameters: params,
options: Options(responseType: ResponseType.bytes),
);
} else if (method == 'POST') {
response = await _dio.post<Uint8List>(
url!,
data: params,
options: Options(responseType: ResponseType.bytes),
);
}
if (debug) {
debugPrint('HTTP STATUS: ${response.statusCode}');
//print(response.data);
}
return Image.memory(response.data!);
} on DioException catch (e) {
debugPrint('dio error. function: $function, error: ${e.error ?? ''}');
throw NsgApiException(NsgApiError(code: 1, message: e.error?.toString() ?? 'Internet connection error', errorType: e.type));
} catch (e) {
debugPrint('network error. function: $function, error: $e');
throw NsgApiException(NsgApiError(code: 0, message: '$e'));
}
}