Provider.of<> 返回 null with ChangeNotifierProxyProvider?



在下面的测试代码中,我有一个标志,用于确定是使用ChangeNotificationProvider还是ChangeNotificationProxyProvider。当我按下RaisedButton时,两种方法都会正确显示我的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.of仅在使用ChangeNotificationerProvider时返回我的GroupEditorProvider实例。当使用Change ChangeNotificationerProxyProvider时,下面的groupEditorProvidernull

class GroupEditorPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final groupEditorProvider = Provider.of<GroupEditorProvider>(context);

我使用Provider已经有一段时间了,但我是ChangeNotificationProxyProvider的新手,所以可能不理解一些基本的东西。

原来我没有从GroupEditorProvider.update函数返回提供程序实例:

update(CloudServicesProvider cloudServicesProvider) {
if (_cloudServicesProvider == null) {
this._cloudServicesProvider = cloudServicesProvider;
}
return this; // <--- was missing
}

Flutter应该为此抛出异常吗?如果是的话,我会发到github。

最新更新