fetchly 0.0.2
fetchly: ^0.0.2 copied to clipboard
Fetchly provides a streamlined and user-friendly approach to harnessing the power of Dio within your Flutter applications.
Fetchly is a lightweight Dart package designed to streamline your API interactions by simplifying the usage of the Dio HTTP client. It provides an easy-to-use, type-safe, and efficient wrapper around Dio, making your HTTP calls and response handling smooth and hassle-free.
Usage #
To use this plugin, add fetchly as a dependency in your pubspec.yaml file.
Example #
void main() {
Fetchly.init(
baseUrl: 'https://dummyjson.com/',
onRequest: (path, status, data){
// listen request
},
onError: (e, s){
// listen error
}
);
runApp(const MyApp());
}
Usage #
To make API requests, extend the Fetchly class and define your methods like so:
import 'package:fetchly/fetchly.dart';
class TodoApi extends Fetchly {
Future<ResHandler> getTodos() async => await get('todos');
}
In this code snippet, we're using Dart's mixin feature to create a reusable block of code named UseApi.
mixin UseApi {
TodoApi todoApi = TodoApi();
// other api
}
class MyClass with UseApi {
Future getTodos() async {
ResHandler res = await todoApi.getTodos();
}
}
Methods #
Provide some methods that you can use
// The `init` method is used to initialize Fetchly with a baseUrl and callbacks for onRequest and onError.
Fetchly.init();
// The `setHeader` method is used to set headers such as Authorization.
// You can also use dio.setToken('your_token') to set a token.
Fetchly.setHeader({});
For more information and examples, see the example.