surav 0.0.13 copy "surav: ^0.0.13" to clipboard
surav: ^0.0.13 copied to clipboard

Quick Dio Request

Surav Usage Guide #

This guide provides an overview of how to use the ApiRequest class and the SuravLocal class for making HTTP requests and managing local data storage in a Flutter application using the Dio package.

Table of Contents #

Installation #

To use the ApiRequest and SuravLocal classes, you need to include the following dependencies in your pubspec.yaml file:

dependencies:
  dio: ^5.0.0
  shared_preferences: ^2.0.6
  logger: ^1.1.0
  flutter:
    sdk: flutter

Run flutter pub get to install the dependencies.

Usage #

Creating an ApiRequest #

To create an instance of the ApiRequest class, you need to provide the URL and optional parameters such as method, body, options, and others.

ApiRequest request = ApiRequest(
  'https://example.com/api/endpoint',
  method: ApiMethod.POST,
  body: {'key': 'value'},
  showLogs: true, // Set to false to disable logging
);

Sending Requests #

To send a request, use the send method. This method returns a Future<Response>.

Response response = await request.send();

Handling Responses #

The send method returns a Dio Response object. You can handle the response as needed:

if (response.statusCode == 200) {
  // Handle success
  print(response.data);
} else {
  // Handle error
  print('Request failed with status: ${response.statusCode}');
}

Logging #

Logging is handled by the Logger package. The logging behavior depends on the request method and whether logging is enabled.

  • GET requests: Informational logs
  • POST and DELETE requests: Warning logs
  • Other methods: Fatal logs

Logs include stack traces for better debugging.

SuravLocal Class #

The SuravLocal class provides methods for managing local data storage using the shared_preferences package.

Setting Data #

To set a value in local storage, use one of the following methods:

await SuravLocal.setString('key', 'value');
await SuravLocal.setBool('key', true);
await SuravLocal.setInt('key', 123);
await SuravLocal.setDouble('key', 123.45);
await SuravLocal.setStringList('key', ['value1', 'value2']);

Getting Data #

To retrieve a value from local storage, use one of the following methods:

String? stringValue = await SuravLocal.getString('key');
bool? boolValue = await SuravLocal.getBool('key');
int? intValue = await SuravLocal.getInt('key');
double? doubleValue = await SuravLocal.getDouble('key');
List<String>? stringListValue = await SuravLocal.getStringList('key');

Clearing Data #

To remove a specific value or clear all data from local storage, use the following methods:

await SuravLocal.remove('key');
await SuravLocal.clear();

Additional Functions #

packFormData #

The packFormData function helps in packing data into a FormData object for multipart/form-data requests.

FormData formData = packFormData({
  'name': 'John Doe',
  'file': await addFormFile('/path/to/file'),
});

addFormFile #

The addFormFile function helps in adding a file to the FormData.

MultipartFile file = await addFormFile('/path/to/file', filename: 'file.txt');
1
likes
0
pub points
0%
popularity

Publisher

unverified uploader

Quick Dio Request

License

unknown (license)

Dependencies

dio, flutter, logger, shared_preferences

More

Packages that depend on surav