title: Add Dio Interceptor to Flutter Riverpod
body: <p>I'm making requests to a server but after some time the <code>access token</code> expires. I'm using Dio and recently came across interceptors. How do I add an interceptor to all calls and fetch <code>access token</code> using the <code>refresh token</code> when a <code>401</code> is returned.
  I'm storing my <code>tokens</code> in a <code>Shared Preferences</code>.Here's my code.</p>
  <pre><code>
  
  class AuthenticationService {
    final Dio _dio;
    final LocalDBService _prefs;
  
    AuthenticationService(this._dio, this._prefs);
  
    Future&lt;User?&gt; fetchUser() async {
      try {
        Tokens _tokens = await _prefs.getTokens();
        if (_tokens.accessToken == '') {
          return null;
        }
        final result = await _dio.get(
          CURRENT_USER,
          options: Options(headers: {
            &quot;Authorization&quot;: &quot;Bearer ${_tokens.accessToken}&quot;,
            'Accept': &quot;application/json&quot;,
          }),
        );
        User _user = User.fromJson(result.data);
        return _user;
      } catch (e) {
        print(&quot;fetcherror&quot;);
        print(e);
        return null;
      }
    }
  }
  
  final authenticationServiceProvider = Provider.autoDispose&lt;AuthenticationService&gt;((ref) {
    final _prefs = ref.read(localDBProvider);
    final _dio = Dio(); // Need to add interceptors to this
    return AuthenticationService(_dio, _prefs);
  });
  
  final userProvider = FutureProvider.autoDispose&lt;User?&gt;((ref) {
    ref.maintainState = true;
    return ref.read(authenticationServiceProvider).fetchUser();
  });
  </code></pre>
  <p>How and where do I add the interceptors. Would also want to move the <code>Dio()</code> instance to its own class</p>