easy_dio_client 0.0.1
easy_dio_client: ^0.0.1 copied to clipboard
A simple and opinionated Dio-based HTTP client for Flutter with built-in authentication session handling, token refresh support, unified API responses, file upload/download helpers, and model conversi [...]
easy_dio_client #
A simple and opinionated Dio wrapper for Flutter that provides:
- Unified API responses
- Automatic token refresh handling
- Secure token storage
- File upload & download helpers
- Pagination parsing
- Model conversion using
convert<T>() - Global request lifecycle hooks
Built on top of dio.
โจ Features #
- GET, POST, PUT, PATCH, DELETE helpers
- File upload (path or bytes)
- File download
- Token refresh handling
- Automatic auth session management
- Pagination extraction
- Global request/response hooks
- Custom interceptors support
๐ฆ Installation #
dependencies:
easy_dio_client: ^0.0.1
โ๏ธ Initialization #
Call DioClient.init() once in main():
void main() async {
WidgetsFlutterBinding.ensureInitialized();
DioClient.init(
baseUrl: "https://api.example.com",
refreshTokenEndpoint: "/auth/refresh",
refreshTokenEndpointKey: "refresh_token",
accessTokenKey: "access_token",
refreshTokenKey: "refresh_token",
accessTokenExpiryKey: "access_token_expiry",
refreshTokenExpiryKey: "refresh_token_expiry",
onAuthFailure: () {
debugPrint("User logged out");
},
);
runApp(const MyApp());
}
๐ฆ App Flow (Splash โ Login โ Home) #
The example app follows this flow:
- SplashScreen checks
isLoggedIn() - If true โ HomePage
- If false โ LoginPage
Splash Screen Example #
class SplashScreen extends StatefulWidget {
const SplashScreen({super.key});
@override
State<SplashScreen> createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
@override
void initState() {
super.initState();
_checkLogin();
}
Future<void> _checkLogin() async {
await Future.delayed(const Duration(seconds: 5));
final isLoggedIn = await DioClient.isLoggedIn();
if (!mounted) return;
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (_) => isLoggedIn ? const HomePage() : const LoginPage(),
),
);
}
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(child: Container(
padding: const EdgeInsets.all(30),
decoration: BoxDecoration(
color: Colors.purple,
borderRadius: BorderRadius.circular(30),
),
child: Text("Dio Client"),
)),
);
}
}
๐ Login Example #
final response = await DioClient().post<Map<String, dynamic>>(
"/login",
data: {
"email": "test@mail.com",
"password": "123456",
},
);
๐ช Logout #
await DioClient.logout();
๐ก Basic Requests #
GET + convert to model #
final response = await DioClient().get<UserModel>(
"/users",
dataKey: "data",
convert: (json) => UserModel.fromJson(json),
);
final users = response.datas;
POST #
await DioClient().post("/users", data: {"name": "John"});
PUT #
await DioClient().put("/users/2", data: {"name": "Updated"});
PATCH #
await DioClient().patch("/users/2", data: {"job": "Engineer"});
DELETE #
await DioClient().delete("/users/2");
๐ค Upload File #
await DioClient().uploadFile(
"/upload",
filePath: "/storage/emulated/0/Download/image.png",
);
๐ฅ Download File #
await DioClient().downloadFile(
"/storage/emulated/0/Download/file.pdf",
"/files/download",
);
๐ ApiResponse #
ApiResponse<T> {
bool success;
bool skipped;
String? message;
String? error;
int? statusCode;
dynamic fullData;
T? data;
List<T>? datas;
Pagination? pagination;
}
๐งช Example App #
The example app demonstrates:
- Splash screen login check
- Login flow
- Session persistence using
isLoggedIn() - Logout handling
- GET, POST, PUT, PATCH, DELETE
- File upload & download
- JSON โ Model mapping using
convert<T>()
See example/main.dart.
๐ License #
MIT