How to update a get it instance in flutter when replace token expires with new token?

335 Views Asked by At
instance.registerLazySingleton<DioFactory>(() => DioFactory(instance()));

  // app  service client
  final dio = await instance<DioFactory>().getDio();
  instance.registerLazySingleton<AppServiceClient>(() => AppServiceClient(dio));

The getDio function return intance Dio().

Future<Dio> getDio() async {
    Dio dio = Dio();
    int _timeOut = 60 * 1000; // 1 min
    String language = await _appPreferences.getAppLanguage();
    Map<String, String> headers = {
      CONTENT_TYPE: APPLICATION_JSON,
      ACCEPT: APPLICATION_JSON,
      AUTHORIZATION: Constants.token,//update new token 
      DEFAULT_LANGUAGE: language
    };

    dio.options = BaseOptions(
        baseUrl: Constants.baseUrl,
        connectTimeout: _timeOut,
        receiveTimeout: _timeOut,
        headers: headers);

    if (kReleaseMode) {
    } else {
      dio.interceptors.add(PrettyDioLogger(
          requestHeader: true, requestBody: true, responseHeader: true));
    }

    return dio;
  }

I tried getIt.allowReassignment = true; and getIt.resetLazySingleton<NetWorkMode>(); getIt.registerLazySingleton<DioFactory>( () => DioFactory(instance());

But the headers next time the API call stays the same with the old token.

I need header update when updated new token and return new Dio intance for call API next time.

0

There are 0 best solutions below