Ecosyste.ms: Awesome
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: about 1 month ago
JSON representation
Flutter Error Handling
- Host: GitHub
- URL: https://github.com/dariomatias-dev/flutter_error_handling
- Owner: dariomatias-dev
- License: mit
- Created: 2024-06-28T21:19:54.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-07-12T15:42:08.000Z (6 months ago)
- Last Synced: 2024-10-13T08:41:23.382Z (2 months ago)
- Topics: dart, dartlang, flutter, mit, mit-license, solution
- Language: C++
- Homepage:
- Size: 59.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Request Results Management in Flutter
This project introduces a custom solution for efficiently handling request results in Flutter applications. Leveraging the `Either` abstraction, the system elegantly manages both success and failure scenarios, allowing for detailed handling of each case. Additionally, it includes a robust global error management system to ensure a seamless user experience without interruptions.
## Requests
### Defining URLs
In `ApiService`, define the base URLs that will be used, using `http` in the same scheme as `http`:
```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
static 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
result.handle(
getContext: _getContext,
success: (value) {},
failure: (type, message, handleError) {},
);
```When a failure occurs and you want the default handling to be performed, call the `handleError` function:
```dart
result.handle(
getContext: _getContext,
success: (value) {},
failure: (type, message, handleError) {
handleError();
},
);
```The default handling will only be applied when `handleError` is called, allowing for local handling as follows:
```dart
result.handle(
getContext: _getContext,
success: (value) {},
failure: (type, message, handleError) {
switch (type) {
case FailureType.badGateway:
// Local Error Handling
default:
handleError();
}
},
);
```