簡體   English   中英

如何在 flutter 中使用 injectable 和 get_it 來使用注入字符串?

[英]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);
}
}

運行構建運行程序時的消息說 factoryMethod 依賴於不可注入的字符串......那么你將如何注入字符串

通常,您可以使用模塊注入原始類型或第三方類型

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

現在在您的工廠方法或構造函數中用 @named 注釋您的可注入字符串

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM