wat_dio
HTTP wrapper on top of dio for Flutter apps that need JWT bearer auth, refresh-token retry, and a small typed response model.
wat_dio is useful when your app already uses dio and you want one place to:
- attach bearer tokens
- retry requests after
401 Unauthorized - react to expired sessions with a callback
- return a simple
RestModel<T>object
Features
RestServicewrapper aroundDio- Auto-attach
Authorization: Bearer <token>whenidTokenexists - Refresh-token callback for expired access tokens
- Expired-session callback for unrecoverable auth states
- Typed
RestModel<T>response wrapper forget,post,put,patch,delete, anddownload
Installation
Add package to pubspec.yaml:
dependencies:
wat_dio: ^0.0.4
Then install dependencies:
flutter pub get
Quick Start
import 'package:dio/dio.dart';
import 'package:wat_dio/wat_dio.dart';
final dio = Dio(
BaseOptions(
baseUrl: 'https://api.example.com',
validateStatus: (status) => status != null && status < 500,
),
);
final service = RestService(
dioClient: dio,
idToken: '<access-token>',
refreshToken: () async {
// Call your refresh endpoint here.
// Return empty string when session can no longer be refreshed.
return '<new-access-token>';
},
expiredToken: (response, handler) async {
// Clear session, log out user, navigate to login, etc.
handler.next(response);
},
);
final result = await service.get<Map<String, dynamic>>(
endpoint: '/profile',
);
print(result.statusCode);
print(result.body);
await service.patch<Map<String, dynamic>>(
endpoint: '/profile',
data: {
'nickname': 'wat',
},
);
Auth Flow
wat_dio currently combines request handling in RestService with auth recovery in WatInterceptor.
RestServicesends request with current bearer token whenidTokenexists.- If server returns
401andrefreshTokenexists, package triesrefreshToken(). - If refresh returns non-empty token, package retries request with new bearer token on same configured
Dioclient. - If refresh fails or auth state is unrecoverable, package calls
expiredToken(...).
See more:
API Overview
Public exports from package:
RestServiceRestModel<T>diotypes frompackage:dio/dio.dart
Main methods on RestService:
Future<RestModel<R>> get<R>({...})
Future<RestModel<R>> post<R>({...})
Future<RestModel<R>> put<R>({...})
Future<RestModel<R>> patch<R>({...})
Future<RestModel<R>> delete<R>({...})
Future<RestModel<R>> download<R>({...})
RestModel<T> shape:
class RestModel<T> {
final Map<String, dynamic> headersModel;
final T body;
final int statusCode;
}
Current Behavior Notes
This package works today, but current release behavior is important to understand before production use:
refreshTokenis optional. When omitted, requests still work but no automatic refresh happens on401.get,post,put,patch,delete, anddownloadnow follow same auth-refresh contract.- Retry logic reuses same configured
Dioclient, so base URL, adapter, and client configuration stay intact. expiredToken(...)still owns unrecoverable auth behavior such as logout or session reset.
Those details are documented so consumers know current behavior, and so future changes can improve package consistency without surprise.
Example
Small demo app lives in example/.
Current example is minimal. Planned direction:
- mock login flow
- persisted access token
- simulated
401-> refresh -> retry path - expired session handling
Contributing
When changing auth behavior, update docs in same pull request:
README.mddoc/auth-flow.mddoc/api-reference.mddoc/roadmap.md
License
See LICENSE.