Flutter 供应商<&燃气轮机;使用ChangeNotifierProxyProvider返回null?
在下面的测试代码中,我有一个标志,用于确定是使用ChangeNotifierProvider还是使用ChangeNotifierProxy Provider。当我按下Flutter 供应商<&燃气轮机;使用ChangeNotifierProxyProvider返回null?,flutter,flutter-change-notifier,Flutter,Flutter Change Notifier,在下面的测试代码中,我有一个标志,用于确定是使用ChangeNotifierProvider还是使用ChangeNotifierProxy Provider。当我按下raised按钮时两种方法都会正确显示我的GroupEditorPage const isUsingChangeNotifierProxyProvider = true; class GroupsPage extends StatelessWidget { showGroupEditor(BuildContext contex
raised按钮时
两种方法都会正确显示我的GroupEditorPage
const isUsingChangeNotifierProxyProvider = true;
class GroupsPage extends StatelessWidget {
showGroupEditor(BuildContext context) {
Navigator.push(
context,
MaterialPageRoute(builder: (_) {
return isUsingChangeNotifierProxyProvider
? ChangeNotifierProxyProvider<CloudServicesProvider,
GroupEditorProvider>(
create: (_) => GroupEditorProvider(),
update: (_, cloudServicesProvider, groupEditorProvider) =>
groupEditorProvider.update(cloudServicesProvider),
child: GroupEditorPage(),
)
: ChangeNotifierProvider<GroupEditorProvider>(
create: (_) => GroupEditorProvider(),
child: GroupEditorPage(),
);
}),
);
}
@override
Widget build(BuildContext context) {
return SliversPage(
text: 'Testing',
sliverList: SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return RaisedButton(
child: Text('+Create Group'),
onPressed: () => showGroupEditor(context),
);
},
childCount: 1,
),
),
);
}
}
我已经使用Provider一段时间了,但我对ChangeNotifierProxy Provider还不熟悉,所以可能不了解一些基本的内容。结果表明,我没有从我的
GroupEditorProvider返回Provider实例。update
函数:
update(CloudServicesProvider cloudServicesProvider) {
if (_cloudServicesProvider == null) {
this._cloudServicesProvider = cloudServicesProvider;
}
return this; // <--- was missing
}
更新(CloudServicesProvider CloudServicesProvider){
if(_cloudServicesProvider==null){
这。_cloudServicesProvider=cloudServicesProvider;
}
归还这个//
update(CloudServicesProvider cloudServicesProvider) {
if (_cloudServicesProvider == null) {
this._cloudServicesProvider = cloudServicesProvider;
}
return this; // <--- was missing
}