Refresh Token For Dio

dio_refresh_bot is an interceptor that attempts to simplify custom API authentication by transparently integrating token refresh and caching.

dio_refresh_bot: is flexible and is intended to support custom token refresh mechanisms.

A dio interceptor for a built-in token refresh.

Getting started

Add dependency #

  dio_refresh_bot: ^1.0.0 #latest version


  • Create a new instance from Dio
final dio = Dio();
  • Creat new class whose has extends from BotMemoryTokenStorage
class TokenStorageImpl extends BotMemoryTokenStorage<AuthToken> {}

and you will get override AuthToken init

// and you will get overrid AuthToken init
  AuthToken? get initValue => const AuthToken(
        accessToken: '<Your Initial Access Token>',
        refreshToken: '<Your Initial Refresh Token>',
        tokenType: '<Your Initial Token Type>',
        // You Can make the token expire in your code
        // without expiring it from the API call (Optional)
        expiresIn: Duration(days: 1),


AuthToken? get initValue => null;
  • Then Create a new intance from TokenStorageImpl
  final storage = TokenStorageImpl();
  • Then Add RefreshTokenInterceptor to Dio Interceptors
      // pass your dio instance 
      dio: dio,
      // pass your storage instance 
      tokenStorage: storage,
      // we have sperable instance for Dio
      // [tokenDio] you can get your dio instance for refresh method
			// [token] is AuthToken object storage
      refreshToken: (token, tokenDio) async {
        final response = await<dynamic>(
          data: {'refreshToken': token.refreshToken},
        return AuthToken.fromMap( as Map<String, dynamic>);

Additional information

We add listen for your storage when your token has Created, Updated, or Deleted

// listen to the token changes;

// listen to auth state changes