我像这样调用多个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);
}
}
1条答案
按热度按时间6gpjuf901#
您可以创建变量字符串令牌
然后,将此变量传递给data[token],而不是**(data ['access_token'])**
之后,将此令牌保存在sharedprrf或任何本地数据中,并在主屏幕中调用get save this token此格式将确保您打开的每个应用程序都会刷新令牌
此设置令牌保存按钮
// value = model.accesstoken
无论模型是什么样子,access_token参数对它都是可见的
this get token from shared pref