tbib_dio_extension 1.1.10
tbib_dio_extension: ^1.1.10 copied to clipboard
Init Dio With pretty logger to log errors and return you errors dio if url with http will get data.
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:tbib_dio_extension/tbib_dio_extension.dart';
void main() {
DioManger.init();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({super.key, required this.title});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class TokenInterceptor extends Interceptor {
@override
Future<void> onError(
DioException err,
ErrorInterceptorHandler handler,
) async {
if (err.response?.statusCode == 403 || err.response?.statusCode == 401) {
// remove cache login and navigation to login
}
super.onError(err, handler);
}
@override
Future<void> onRequest(
RequestOptions options,
RequestInterceptorHandler handler,
) async {
// get user token and send it to header
super.onRequest(options, handler);
}
@override
Future<void> onResponse(
Response response,
ResponseInterceptorHandler handler,
) async {
super.onResponse(response, handler);
}
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
// This trailing comma makes auto-formatting nicer for build methods.
);
}
void getDataApi() async {
var dio = DioManger.dioApi;
try {
debugPrint("dio url is ${(dio.options.baseUrl)}");
var res = await dio.post('/endpoint', data: {});
debugPrint("dio url is ${res.realUri}");
res.data;
} catch (error) {
debugPrint(
"error catch is ${ErrorHandler.handle(error, messageFromApi: (error is DioException) ? error.response?.data['errorMessage'] : null).failure.messages}");
}
}
@override
void initState() {
getDataApi();
super.initState();
}
}