这是我想注入WebParserApi
和ProxyProvider的简单类,但我得到了null值。
class RetrievePageSummarizedInformation extends StatesRebuilder {
BuiltUserProfile builtUserProfile = BuiltUserProfile();
final WebParserApi _api;
RetrievePageSummarizedInformation({WebParserApi api}) : _api = api;
retrievePageProfileInfo(BuildContext context,String username) async {
//_preparePageProfileCache();
//WebParserApi _api = Provider.of<WebParserApi>(context);
return await _api.getProfileRetrieveFromParser(username);
}
void _preparePageProfileCache() async {
await KvStore().createTable('userProfile');
}
}
主函数:
void main() async {
Provider.debugCheckInvalidValueType = null;
_setUpLogging();
runApp(MultiProvider(providers: providers, child: OKToast(child: StartupApplication())));
}
proxyProvider
实现:
List<SingleChildCloneableWidget> providers = [
...independentServices,
...dependentServices,
...uiConsumableProviders
];
List<SingleChildCloneableWidget> independentServices = [
Provider(
builder: (_) => WebParserApi.create(),
dispose: (_, WebParserApi service) => service.client.dispose(),
)
];
List<SingleChildCloneableWidget> dependentServices = [
ProxyProvider<WebParserApi, RetrievePageSummarizedInformation>(
builder: (context, api, retrievePageSummarizedInformation) => RetrievePageSummarizedInformation(api: api),
)
];
List<SingleChildCloneableWidget> uiConsumableProviders = [
];
这是我的WebParserApi
类的实现:
@ChopperApi(baseUrl: '/')
abstract class WebParserApi extends ChopperService {
@Get(path: '{token}')
Future<Response<BuiltUserProfile>> getProfileRetrieveFromParser(@Path() String username);
static WebParserApi create() {
final client = ChopperClient(
client: http.IOClient(
HttpClient()..connectionTimeout = const Duration(seconds: 60),
),
baseUrl: 'https://www.sample.com',
services: [
_$WebParserApi(),
],
converter: BuiltValueConverter(),
interceptors: [
HeadersInterceptor({'Content-Type': 'application/json'}),
HttpLoggingInterceptor(),
]);
return _$WebParserApi(client);
}
}
问题出在这里。将WebParserApi
注入到 RetrievePageSummarizedInformation
类中:
return Injector<RetrievePageSummarizedInformation>(
models: [() => RetrievePageSummarizedInformation()],
builder: (context, model) => Stack(
children: <Widget>[
),
],
),
);
我在GitHub上创建了一个简单的项目,链接为https://github.com/MahdiPishguy/proxy_provider_sample。当我尝试在主类中使用_pageInformation.pageProfile('test')时,PageInformation类中的_api变量为空。
RetrievePageSummarizedInformation
类中,_api
为空。 - DolDurmaWebParserApi
传递给RetrievePageSummarizedInformation
类的构造函数作为_api
参数。 - DolDurmahttps://github.com/MahdiPishguy/proxy_provider_sample
。 在main
类中使用_pageInformation.pageProfile('test');
时,在PageInformation
类的_api
变量为空。 - DolDurma