busenet

Creator: coderz1093

Last updated:

0 purchases

busenet Image
busenet Images

Languages

Categories

Add to Cart

Description:

busenet

This package is a custom network layer for Flutter that leverages the popular Dio HTTP client library to manage API requests. It provides a generic response model and supports optional request-based caching. The caching feature reduces unnecessary network traffic and improves application performance when the same API request is made multiple times.
With this package, you can easily manage your application's network layer and develop faster, more efficient applications.
Features #

Generic response model
Request-based caching
Customizable request headers, query parameters, and request body, including support for multipart file uploads.

Getting Started #
Installation #
Add the following dependency to your pubspec.yaml file:
dependencies:
busenet: <latest-version>
copied to clipboard
Usage #
Import the package
import 'package:busenet/busenet.dart';
copied to clipboard
Create a new Network Manager instance
Let's create two different response model examples for two different projects. Both of our models must inherit from BaseResponse.
BaseResponse
abstract class BaseResponse<T> {
int? statusCode;
String? errorMessage;
Failure? errorType;

T fromJson(Map<String, dynamic> json);
void setData<R>(R entity);
}
copied to clipboard
FirstResponseModel
class FirstResponseModel extends BaseResponse<FirstResponseModel> {
dynamic entity;
dynamic error;
bool? success;
String? message;

ResponseModel({
this.entity,
this.error,
this.success,
this.message,
});

@override
void setData<R>(R entity) {
this.entity = entity;
}

@override
ResponseModel fromJson(Map<String, dynamic> json) {
return ResponseModel.fromMap(json);
}

Map<String, dynamic> toMap() {
return <String, dynamic>{
'entity': entity,
'error': error,
'success': success,
'message': message,
};
}

factory ResponseModel.fromMap(Map<String, dynamic> map) {
return ResponseModel(
entity: map['entity'] as dynamic,
error: map['error'] as dynamic,
success: map['success'] != null ? map['success'] as bool : null,
message: map['message'] != null ? map['message'] as String : null,
);
}

String toJson() => json.encode(toMap());

factory ResponseModel.fromJson(String source) => ResponseModel.fromMap(
json.decode(source) as Map<String, dynamic>,
);
}
copied to clipboard
SecondResponseModel
class SecondResponseModel extends BaseResponse<SecondResponseModel> {
dynamic body;
// int? statusCode; already exists in the class it inherits

SecondResponseModel({
required this.body,
});

@override
SecondResponseModel fromJson(Map<String, dynamic> json) {
return SecondResponseModel.fromMap(json);
}

@override
void setData<R>(R entity) {
body = entity;
}

Map<String, dynamic> toMap() {
return <String, dynamic>{
'body': body,
};
}

factory SecondResponseModel.fromMap(Map<String, dynamic> map) {
return SecondResponseModel(
body: map['body'] as dynamic,
);
}

String toJson() => json.encode(toMap());

factory SecondResponseModel.fromJson(String source) =>
SecondResponseModel.fromMap(json.decode(source) as Map<String, dynamic>);
}
copied to clipboard
Let's create an instance now. We are adding the property name where the data will be set for both models to entityKey.
...
INetworkManager manager = NetworkManager<FirstResponseModel>()
..initialize(
NetworkConfiguration('<base-url>'),
responseModel: FirstResponseModel(),
cacheStoreKey: 'boilerplate_cache',
entityKey: 'entity',
);
...

or

...
INetworkManager manager = NetworkManager<SecondResponseModel>()
..initialize(
NetworkConfiguration('<base-url>'),
responseModel: SecondResponseModel(),
cacheStoreKey: 'boilerplate_cache',
entityKey: 'body',
);
...
copied to clipboard
Important Note:
If you don't have a custom response model architecture for your project, you can create an instance using EmptyResponseModel. Please take a look at the example project.
,,,
INetworkManager manager = NetworkManager<EmptyResponseModel>()
..initialize(
NetworkConfiguration('<base-url>'),
responseModel: EmptyResponseModel(),
cacheStoreKey: 'example-cache',
);
...
copied to clipboard
Make a request
...
final response = await manager.fetch<SamplePostModel, SamplePostModel>(
'${NetworkPaths.getSamplePost}/1',
parserModel: const SamplePostModel(),
type: HttpTypes.get,
cachePolicy: CachePolicy.forceCache, // optional - defaults to CachePolicy.noCache
maxStale: const Duration(seconds: 10), // optional - defaults to 10 seconds if cachePolicy is CachePolicy.forceCache
ignoreEntityKey: true, // optional - defaults to false -> if true, the data will not be set to the entityKey property of the response model
) as SampleResponseModel;

switch (response.statusCode) {
case 1:
return response.data as SamplePostModel;
default:
return null;
}
...
copied to clipboard
Make a primitive request
...
final response = await manager.fetchPrimitive<String, List<String>>(
'<sample-url>',
type: HttpTypes.get,
cachePolicy: CachePolicy.forceCache, // optional - defaults to CachePolicy.noCache
maxStale: const Duration(seconds: 10), // optional - defaults to 10 seconds if cachePolicy is CachePolicy.forceCache
ignoreEntityKey: true, // optional - defaults to false -> if true, the data will not be set to the entityKey property of the response model
) as SampleResponseModel;

switch (response.statusCode) {
case 1:
return response.data as SamplePostModel;
default:
return null;
}
...
copied to clipboard
License #
This project is licensed under the MIT License - see the LICENSE file for details

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.