Skip to content
pubscout

fresh_dio

0.6.0, released 9 months ago by felangel.dev

A dio interceptor for automatic token refresh and authentication management.

WASM-ready
$ flutter pub add fresh_dio
Source repository

What it does

Fresh is an HTTP interceptor built on top of package:dio that manages authentication tokens transparently. It automatically refreshes expired tokens, attaches auth headers, handles 401 errors by retrying requests, and provides an authentication status stream.

AI-written summary of version 0.6.0, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You are using dio for networking and need automatic token refresh on 401 responses.
  • You need single-flight request queuing during token refresh.
  • You want to react to authentication status changes via a stream.

Skip it when

  • Not documented
  • You are not using package:dio for your networking layer.

Strengths

  • Automatic token refresh and proactive refreshing before requests
  • Single-flight refresh prevents duplicate concurrent refresh calls
  • Pluggable token storage and custom token formats supported

Weaknesses

  • Tied specifically to package:dio

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
2
final dio = Dio();
dio.interceptors.add(
  Fresh.oAuth2(
    tokenStorage: InMemoryTokenStorage<OAuth2Token>(),
    refreshToken: (token, client) async {
      final response = await client.post(
        'https://api.example.com/auth/refresh',
        data: {'refresh_token': token?.refreshToken},
      );
      final body = response.data as Map<String, dynamic>;
      return OAuth2Token(
        accessToken: body['access_token'],
        refreshToken: body['refresh_token'],
      );
    },
  ),
);

Alternatives

Packages that do the same job and could replace fresh_dio.

Works with

Packages commonly used together with fresh_dio: ones it builds on, ones built on it, and its own family.