Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/multicaret/mc_dio_wrapper
This project not only works perfectly with Laravel projects but also provides the ability to work with other APIs. It aims to make coding easier, especially when using Laravel.
https://github.com/multicaret/mc_dio_wrapper
dart-package dio dio-interceptor flutter flutter-examples http-client http-requests larave
Last synced: 16 days ago
JSON representation
This project not only works perfectly with Laravel projects but also provides the ability to work with other APIs. It aims to make coding easier, especially when using Laravel.
- Host: GitHub
- URL: https://github.com/multicaret/mc_dio_wrapper
- Owner: multicaret
- License: other
- Created: 2024-09-13T21:41:07.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-11-24T01:05:13.000Z (29 days ago)
- Last Synced: 2024-11-24T02:18:29.685Z (29 days ago)
- Topics: dart-package, dio, dio-interceptor, flutter, flutter-examples, http-client, http-requests, larave
- Language: Dart
- Homepage: https://multicare.com
- Size: 79.1 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# mc_dio_wrapper
A Flutter package for Multicaret projects that simplifies HTTP requests, logging, error handling, and caching.
## Features
- Simplified HTTP requests
- Built-in request/response logging
- Error handling
- Cache management
- Toast notifications## Installation
Add `mc_dio_wrapper` to your `pubspec.yaml` file:
```yaml
dependencies:
mc_dio_wrapper: latest_version
```Run `flutter pub get` to install the package.
## Usage
### Basic Initialization
To use `mc_dio_wrapper`, initialize it in your Flutter application:
```dart
import 'package:mc_dio_wrapper/mc_dio_wrapper.dart';void main() {
// ...
McHttpWrapperInitializer.by(
baseUrl: const String.fromEnvironment('API_ORIGIN', defaultValue: 'https://dummyjson.com'),
);
// ...
runApp(const MyApp());
}
```### Making HTTP Requests
Here’s how to make a basic HTTP GET request:
```dart
import 'package:example/models/user_index_response.dart';
import 'package:mc_dio_wrapper/mc_dio_wrapper.dart';import 'api_endpoints.dart';
class AppHttpController {
final McHttpWrapper _httpService = McHttpWrapper();Future> fetchUser() async {
try {
McResponse response = await _httpService.get(
AppApiEndpoints.userIndex.path,
converter: UserIndexResponse.fromJson,
queryParameters: HttpRequestPayload.fromMap({'limit': 2}),
);return response.data!.data.users;
} catch (e) {
rethrow;
}
}
}```
### Enabling Features
Enable various features such as logging, headers, API version, and caching for your HTTP requests and responses:
```dart
void main() {
/// ...
McHttpWrapperInitializer.by(
baseUrl: const String.fromEnvironment('API_ORIGIN', defaultValue: 'https://dummyjson.com'),
isLocalizedApi: false,
httpLoggerLevel: LogDetails.full,
headers: {
'Authorization': 'Bearer YOUR_API_TOKEN',
'Content-Type': 'application/json', // <-- This is already defined in the default header
},
apiVersion: 'v1',
enableCaching: true,
);/// ...
}
```### Error Handling Example 1
Set up custom error handling using dialog:
```dart
/// Inside your widget
_loadData() {
_controller.fetchUsers().then((List users) {
if (kDebugMode) {
print('Users Length => ${users.length}');
}
}).onError(McDioError.handlerByDialogByErrors(context));
}
```### Error Handling Example 2
Set up custom error handling using toast error message:
```dart
/// Inside your widget
_loadData(BuildContext context) {
_controller.fetchUsers().then((List users) {
if (kDebugMode) {
print('Users Length => ${users.length}');
}
}).onError(McDioError.handlerByToastMsg);
}
```### Steps to Use the Example Project
1. **Clone the repository** and navigate to the `example` directory.
2. **Run `flutter pub get`** to install dependencies.
3. **Run the example app** using `flutter run`.## License
This project is licensed under the MIT License.