简体   繁体   中英

How to use inject String using injectable and get_it in flutter?

 @LazySingleton()
 @ChopperApi(baseUrl:"/api")
 abstract class ChatApiService extends ChopperService { 
   
  @Post(path: "/logout")
  Future<Response> logout(
  @Body() Map<String, dynamic> body,);
  
  @factoryMethod
  static ChatApiService create(String accessToken){
   final client = ChopperClient(
    baseUrl: 'localhost:8000',
    services: [_$ChatApiService()],
    converter: JsonConverter(),
    client: http.IOClient(
     HttpClient()..connectionTimeout = const Duration(seconds: 60),
    ),
    interceptors: [
     HeadersInterceptor({
      'Accept': 'application/json',
      'Authorization':'Bearer $accessToken'
    }),
    HttpLoggingInterceptor()
   ]
 );
return _$ChatApiService(client);
}
}

the messange when running build runner says that the factoryMethod depends on String which is not injectable... so how would you inject String

Generally you can inject primitive or third party types using a module

@module
abstract class RegisterModule{
//register the string as a named type
 @Named('accessToken')
 String get accessToken => "myAccessToken";
}

now in your factory method or constructor annotated your injectable string with @named

 @factoryMethod
  static ChatApiService create(@Named('accessToken') String accessToken){}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM