Skip to main content

easy_dio_client 0.0.1 copy "easy_dio_client: ^0.0.1" to clipboard
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:

  1. SplashScreen checks isLoggedIn()
  2. If true โ†’ HomePage
  3. 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

0
likes
0
points
68
downloads

Publisher

unverified uploader

Weekly Downloads

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 conversion using generics.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

dio, flutter, flutter_secure_storage, http_parser

More

Packages that depend on easy_dio_client