An open API service indexing awesome lists of open source software.

https://github.com/dariomatias-dev/flutter_error_handling

Flutter Error Handling
https://github.com/dariomatias-dev/flutter_error_handling

dart dartlang flutter mit mit-license solution

Last synced: 4 months ago
JSON representation

Flutter Error Handling

Awesome Lists containing this project

README

        

# Request Management in Flutter

A custom solution offering efficient and flexible management of requests in Flutter applications. Based on the `Either` abstraction, it simplifies handling success and failure scenarios, enabling detailed and tailored management for each situation.

The solution supports both local management, where specific cases can be handled individually, and global management, which applies consistent rules across all cases. Additionally, a robust global error management system ensures a smooth and uninterrupted user experience, even in unexpected situations.

## Requests

### Defining URLs

In `ApiService`, define the base URLs that will be used, using `http` in the same scheme as `http`:

__Class with URLs:__
```dart
class Urls {
static const httpUrl = 'https://dummyjson.com/http';
}
```

__Service Class:__
```dart
class ApiService {
static ApiMethods get http => ApiMethods(
baseUrl: Urls.httpUrl,
);
}
```

### Creating an Instance for Requests

Create an instance of `ApiService` calling the `get` method on the base URL of the requests:

```dart
final _api = ApiService.http;
```

### HTTP Requests

To make requests, call the corresponding HTTP method as shown below:

```dart
final result = await _api.get('[path]');
```

If there is a header:

```dart
final result = await _api.get(
'[path]',
headers: {},
);
```

When there is a body:

```dart
final result = await _api.post(
'[path]',
headers: {},
data: {},
);
```

## Handling Results

The result of the requests will be an instance of Either, which can be either a success or a failure. To handle the result, access the `handle` method, passing a function to obtain the current context, what to do when a success occurs, and what to do when a failure occurs:

```dart
await result.handle(
getContext: _getContext,
success: (value) {},
failure: (type, message, handleError) async {},
);
```

When a failure occurs and you want the default handling to be performed, call the `handleError` function:

```dart
await result.handle(
getContext: _getContext,
success: (value) {},
failure: (type, message, handleError) async {
await handleError();
},
);
```

The default handling will only be applied when `handleError` is called, allowing for local handling as follows:

```dart
await result.handle(
getContext: _getContext,
success: (value) {},
failure: (type, message, handleError) async {
switch (type) {
case FailureType.badGateway:
// Local Error Handling
default:
await handleError();
}
},
);
```


## Autor

My photo


Dário Matias


Solution developed for study and learning purposes, available under the MIT license.