0 purchases
devla request
devla_request #
A Http client request for Dart, which help you zero configuration interceptor with global stage and more cleaner with BaseURL, Logging request, header Authorization set with Bearer and Auto request when Token has expired
Installing #
Add Get to your pubspec.yaml file:
dependencies:
devla_request: ^0.0.2
copied to clipboard
Import get in files that it will be used:
import 'package:devla_request/devla_request.dart';
copied to clipboard
Using with Getx #
create the new file called index.dart in the
class Controller extends GetxController {
static Controller to = Get.find();
final Dio _dio = new Dio();
@override
void onInit() async {
super.onInit();
_dio.interceptors.add(DevlaRequest(
dio: _dio,
baseUrl: 'https://jsonplaceholder.typicode.com', // enter your baseURL here
));
}
}
copied to clipboard
add init Getx Controller in the main.dart file
Future<void> main() async {
Get.put<Controller>(Controller());
await GetStorage.init();
runApp(MyApp());
}
copied to clipboard
Change the MaterialApp to GetMaterialApp
@override
Widget build(BuildContext context) {
return GetMaterialApp(
...
}
copied to clipboard
Usage with devla_request #
add below function to fetchData into the controllers/index.dart
fetchData() async {
try {
Response response = await _dio.get('/todos/');
return response.data;
} catch (e) {
if (e is DioError) {
print("error fetchData : ${e.response}");
}
}
}
copied to clipboard
full code in the controllers/index.dart going to be like below.
import 'package:devla_request/devla_request.dart';
import 'package:get/get.dart' hide Response;
import 'package:dio/dio.dart';
class Controller extends GetxController {
final Dio _dio = new Dio();
@override
void onInit() async {
super.onInit();
_dio.interceptors.add(DevlaRequest(
dio: _dio,
baseUrl: 'https://jsonplaceholder.typicode.com',
));
}
fetchData() async {
try {
Response response = await _dio.get('/todos/');
return response.data;
} catch (e) {
if (e is DioError) {
print("error fetchData : ${e.response}");
}
}
}
}
copied to clipboard
called function in stateFullWidget #
In this example will use main.dart to called api with controllers/index.dart
Controller _controller = Get.put(Controller());
List<TodosModel> _listItem = [];
@override
void initState() {
super.initState();
fetchData();
}
void fetchData() async {
var data = await _controller.fetchData();
var items = json.encode(data);
_listItem.clear();
// find more example in lib/src/models/todo.dart
_listItem.addAll(todosModelFromJson(items));
setState(() {});
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.