Flutter Retrofit Networking
ASecurityUse a Dio auth interceptor and handle refresh in `onError` when the response is `401`. The usual flow is: 1. Add the access token in `onRequest` 2. If a request fails with `401`, pause or serialize retries 3. Call `refreshToken()` with your refresh token 4. Save the new access token 5. Clone and retry the original request with the updated `Authorization` header 6. If refresh also fails, clear auth state and force logout Example: ```dart class AuthInterceptor extends Interceptor { AuthIntercep...
- 584 stars
- 0 votes
- 0 copies
- 4 views
- Added September 5, 2026
Works with
Security analysis
100/100Pro scans all 12 files and shows the line behind each finding
npx -y skills add HoangNguyen0403/agent-skills-standard --skill flutter-retrofit-networking --agent claude-codeAre you the author of Flutter Retrofit Networking?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-flutter-retrofit-networking-6ee66279)Use a Dio auth interceptor and handle refresh in `onError` when the response is `401`.
The usual flow is:
1. Add the access token in `onRequest`
2. If a request fails with `401`, pause or serialize retries
3. Call `refreshToken()` with your refresh token
4. Save the new access token
5. Clone and retry the original request with the updated `Authorization` header
6. If refresh also fails, clear auth state and force logout
Example:
```dart
class AuthInterceptor extends Interceptor {
AuthInterceptor({
required this.dio,
required this.tokenStore,
required this.refreshToken,
});
final Dio dio;
final TokenStore tokenStore;
final Future<String> Function() refreshToken;
bool _isRefreshing = false;
final List<RequestOptions> _pendingRequests = [];
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
final token = await tokenStore.getAccessToken();
if (token != null) {
options.headers['Authorization'] = 'Bearer $token';
}
handler.next(options);
}
@override
void onError(DioException err, ErrorInterceptorHandler handler) async {
if (err.response?.statusCode != 401) {
return handler.next(err);
}
final request = err.requestOptions;
if (_isRefreshing) {
_pendingRequests.add(request);
return;
}
_isRefreshing = true;
try {
final newAccessToken = await refreshToken();
await tokenStore.saveAccessToken(newAccessToken);
request.headers['Authorization'] = 'Bearer $newAccessToken';
final retryResponse = await dio.fetch(request);
for (final pending in _pendingRequests) {
pending.headers['Authorization'] = 'Bearer $newAccessToken';
dio.fetch(pending);
}
_pendingRequests.clear();
handler.resolve(retryResponse);
} catch (_) {
_pendingRequests.clear();
await tokenStore.clear();
handler.next(err);
} finally {
_isRefreshing = false;
}
}
}
```
If you use Retrofit, keep refresh logic in the Dio interceptor, and inject that configured `Dio` into your `RestClient`.
Important details:
- Do not create a global Dio singleton manually; inject it through DI
- Retry with `dio.fetch(err.requestOptions)` after updating the token
- Prevent multiple simultaneous refresh calls
- If your backend can add new enum values in auth/profile payloads, protect DTO enums with `@JsonKey(unknownEnumValue: ...)`
- Map `DioException` to typed failures in the repository layer
Files in this skill
- eval-1.baseline.md
- eval-1.with-skill.md
- eval-2.baseline.md
- eval-2.with-skill.md
- eval-3.baseline.md
- eval-3.with-skill.md
- trigger-1.md
- trigger-2.md
- trigger-3.md
- trigger-4.md
- trigger-5.md
Attribution
Related skills
View all in development →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.