这是我想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');
}
}
Run Code Online (Sandbox Code Playgroud)
主功能:
void main() async {
Provider.debugCheckInvalidValueType = null;
_setUpLogging();
runApp(MultiProvider(providers: providers, child: OKToast(child: StartupApplication())));
}
Run Code Online (Sandbox Code Playgroud)
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 = [
];
Run Code Online (Sandbox Code Playgroud)
这是我的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);
}
}
Run Code Online (Sandbox Code Playgroud)
问题在这里。注入WebParserApi到RetrievePageSummarizedInformation类:
return Injector<RetrievePageSummarizedInformation>(
models: [() => RetrievePageSummarizedInformation()],
builder: (context, model) => Stack(
children: <Widget>[
),
],
),
);
Run Code Online (Sandbox Code Playgroud)
我在github上的此链接https://github.com/MahdiPishguy/proxy_provider_sample上创建了一个简单项目,因为当我尝试使用_pageInformation.pageProfile('test');时,PageInformation类的_api变量为null。在主要班级
我的问题解决了,我们必须Provider.of(context)在定义的类上使用ProxyProvider
class RetrievePageSummarizedInformation extends StatesRebuilder {
BuiltUserProfile builtUserProfile = BuiltUserProfile();
final WebParserApi _api;
RetrievePageSummarizedInformation({@required WebParserApi api}) : _api = api;
getPageProfileInfo(String username) async {
final res = await _api.getSimpleProfileInformation(username);
return res;
}
}
Run Code Online (Sandbox Code Playgroud)
注射液:
models: [() => RetrievePageSummarizedInformation(api:Provider.of(context))],
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
189 次 |
| 最近记录: |