Retrofit Generator and custom dio Interceptors (with get_it)

187 Views Asked by At

I followed some tutorials to create an architecture in my project and came across a situation. I'm using get_it for my dependency injections and Retrofit with Dio for communication with my API.

I have the following scenario: in some requests, I'll have specific headers (only in some), and I have the following code:

// Part of the code where I perform the dependency injection

final sl = GetIt.instance;
Future<void> inject() async {
  sl.registerSingleton<Dio>(Dio());
  sl.registerSingleton<APIService>(APIService(sl()));
  sl.registerSingleton<OtherAPIService>(OtherService(sl()));
}

Now, the code of my service (API client):

part 'api_service.g.dart';

@RestApi(baseUrl: "https://www...")
abstract class APIService {

  factory APIService(Dio dio, {String baseUrl}) = _APIService;

  @POST('/endpoint1')
  Future<HttpResponse<TestModel>> getData({
     @Query("option") String ? option,
  });

}

As you can see, I am using the Retrofit generator for this service, and only for this service, I add interceptors to Dio. However, as you can see, since I am using dependency injection (get_it), the object is already passed as an argument to my constructor.

I prefer not to modify the code generated by Retrofit (as I need to regenerate the file multiple times)

From what I understand, I will indeed need two instances of Dio and register them in my dependencies, or can I modify my service to implement the changes to Dio directly in my APIService?

0

There are 0 best solutions below