flutterretrofitinjectable

Flutter - using retrofit with injectable


I am trying to use retrofit along with injectable

I am following clean architecture, and in the repo class i want to inject the retrofit client but i cant annotate the client with injectable since its abstract and i cant annotate the implementation because it is generated

I tried to annotate the generated file but that wont work because it will remove my changes the next time build runner is called

is there a proper way to solve this problem

i wont post my code because there isnt much to it


Solution

  • Yeah, you use the @module annotation and create a "RetrofitInjectableModule". As such:

    @module  
    abstract class RetrofitInjectableModule {  
       BackendService getService(ApiClient client, @factoryParam String url) => BackendService(client, url);  
    }  
    

    Where BackendService is your RestClient.

    Read about @module on https://pub.dev/packages/injectable