flutter Dio - QueuedInterceptor用于处理多个请求的刷新令牌

fd3cxomn  于 2023-05-19  发布在  Flutter
关注(0)|答案(1)|浏览(495)

我像这样调用多个API

await Future.wait([profile, calendar, cities]);

我有一个拦截器类,它扩展了Dio****QueuedInterceptor。假设在profile API上,我的访问令牌过期,onError被调用。所以我点击refresh_token API,重试当前请求,成功后我调用handler.resolve(response);。但是其余的API仍然调用拦截器onError方法,我的refresh_token api总共被调用了3次。有什么问题吗?

Future<void> onError(DioError err, ErrorInterceptorHandler handler) async {
    
    if (err.response?.statusCode == 401) {
      var tokenDio = Dio();

      try {
        Response refreshResponse = await tokenDio.post('/${endPoint.path}',
            data: endPoint.requestBody);

        if (refreshResponse.statusCode == 201) {
          var newAccessToken = refreshResponse.data['access_token'];

          var newHeaders = err.requestOptions.headers;
          newHeaders['Authorization'] = 'Bearer $newAccessToken';
          
           var response = await dio.request(
            err.requestOptions.path,
            data: err.requestOptions.data,
            queryParameters: err.requestOptions.queryParameters,
            options: Options(
              method: err.requestOptions.method,
              headers: newHeaders,
            ),
          );
          
          return handler.resolve(response);
        } 
        else {
          return handler.reject(err);
        }
      } catch (error) {
        return handler.reject(err);
      }
    } 
    else {
      return handler.next(err);
    }
  }
6gpjuf90

6gpjuf901#

您可以创建变量字符串令牌
然后,将此变量传递给data[token],而不是**(data ['access_token'])**

late  String token ; // global variable 
var newAccessToken = refreshResponse.data[token];

之后,将此令牌保存在sharedprrf或任何本地数据中,并在主屏幕中调用get save this token此格式将确保您打开的每个应用程序都会刷新令牌

此设置令牌保存按钮

await sharedPref.setString('token' , value);

// value = model.accesstoken
无论模型是什么样子,access_token参数对它都是可见的

this get token from shared pref

void main()async  {
  WidgetsFlutterBinding.ensureInitialized();
  token =  await SharededPref.get('token') ?? '';
  runApp(const MyApp());
}

相关问题