how to redirect user to logout page when token or refresh token expired using bloc in flutter

Viewed 217

This is my base repository where it gets the data from the bloc I want to redirect the user to a custom page like logout when the token expires or refresh token expire. I change pages, in bloc consumer or bloc listener but in this case, this is stupid work because I should replace bloc consumer with another bloc method everywhere I request to the server

 @override
  Future<Map<String, dynamic>> get(String url) async {
    // Uri newUrl = Uri.parse(baseUrl + url);
    var newUrl = baseUrl + url;
    dio.Response response;
    try {
      // x.interceptors.add(dio.InterceptorsWrapper(
      //   onError: (e, handler) {
      //     if(e.response!.statusCode == 401)
      //         redirect user to custom page
      //   },
      // ));
      response = await dio.Dio()
          .get(newUrl, options: dio.Options(headers: headersList))
          .timeout(duration);
      var result = _processResponse(response);
      if (result.runtimeType.toString().toLowerCase() == 'string') {
        return <String, String>{'key': result};
      }
      return result;
    } on SocketException {
      throw FetchDataException('internetConnection');
    } on TimeoutException {
      throw ApiNotRespondingException('api not found ');
    } on Exception catch (e) {
      var response = e as dio.DioError;
      print(e.toString());
      throw _handleError(response);
    }
  }
0 Answers
Related