0% found this document useful (0 votes)
3 views2 pages

Dart Dio REST API Example

The document defines a Dart class named RestAPI that utilizes the Dio package for making HTTP requests. It includes methods for GET, POST, and file upload requests, handling headers and response data appropriately. Error handling is implemented to manage exceptions and return relevant error messages when requests fail.

Uploaded by

anlog268
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Dart Dio REST API Example

The document defines a Dart class named RestAPI that utilizes the Dio package for making HTTP requests. It includes methods for GET, POST, and file upload requests, handling headers and response data appropriately. Error handling is implemented to manage exceptions and return relevant error messages when requests fail.

Uploaded by

anlog268
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import 'dart:convert';

import 'dart:io';

import 'package:dio/[Link]';

class RestAPI {
late Dio _dio;
RestAPI() {
_dio = Dio();
}

Future<Map<String, dynamic>> getRequest(String url,


{Map<String, dynamic>? headers}) async {
Options options = Options(
headers: headers,
);
try {
var response = await _dio.getUri([Link](url), options: options);
print('The get response from the url: [$url] is ${[Link]}');
if ([Link] == 200) {
return [Link]([Link]);
} else {
throw Exception('Failed to load data');
}
} catch (e) {
DioException exception = e as DioException;
if ([Link] != null) {
return [Link]([Link]!.data);
}
rethrow;
}
}

Future<Map<String, dynamic>> postRequest(String url,


{Map<String, dynamic>? headers, Map<String, dynamic>? body}) async {
Options options = Options(
headers: headers,
);
try {
var response = await _dio.postUri(
[Link](url),
options: options,
data: body,
);
print('The post response from the url: [$url] is ${[Link]}');
if ([Link] == 200) {
return [Link]([Link]);
} else {
throw Exception('Failed to load data');
}
} catch (e) {
DioException exception = e as DioException;
if ([Link] != null) {
return [Link]([Link]!.data);
}
rethrow;
}
}
Future<Map<String, dynamic>> uploadPostRequest(String url,
{Map<String, dynamic>? headers,
required Map<String, File>? files,
Map<String, dynamic>? body}) async {
try {
final formData = FormData();
if (body != null) {
[Link]((key, value) {
// Handle list types by converting to JSON string
if (value is List) {
[Link](MapEntry(key, jsonEncode(value)));
} else {
[Link](MapEntry(key, [Link]()));
}
});
}

// Add files
if (files != null) {
for (var entry in [Link]) {
[Link](MapEntry(
[Link],
await [Link](
[Link],
filename: [Link], // UUID filename
),
));
}
}

var response = await _dio.postUri(


[Link](url),
options: Options(headers: headers),
data: formData,
);
print('The post response from the url: [$url] is ${[Link]}');
if ([Link] == 200) {
return [Link];
} else {
throw Exception('Failed to load data');
}
} catch (e) {
print('[DEBUG] Error: $e');
DioException exception = e as DioException;
if ([Link] != null) {
return [Link]([Link]!.data);
}
rethrow;
}
}
}

You might also like