watch(提供者)是否更新父窗口小部件的子项



我试图用一个UserInteraction更新两个页面,因此试图用Riverpod库访问两个页面中的同一个流。

现在进一步解释。当我将Stream传递给CustomerPage时,我能够获得数据(String Anton(。并且当我点击触发FireStore中的改变的按钮时;马可";在ParentWidget中,当我回到它时。但在CustomerPage中,它不会改变,除非我通过ParentWidget中的RaisedButton重新打开页面。但我希望在单击"客户页面"上的按钮后对其进行更新。

我希望这能让事情变得更清楚。

class ParentWidget extends ConsumerWidget{
Widget build(BuildContext context, ScopedReader watch){
Stream<DocumentSnapshot> doc = watch(streamProvider.stream);
return Container(
child: Column(
children: [ 
Text(doc.name), //Lets say the name is Anton,
RaisedButton(
child: Text(" road to CustomerPage"),
onPressed:(){
Navigator.of(context).pushNamed(RouteGenerator.customerPage, arguments: doc);
},), //RaisedButton
],), //Column
);  //Container
}
}
class CustomerPage extends StatelessWidget{
Stream<DocumentSnapshot> docStream
CustomerPage({this.docStream});
Widget build(BuildContext context){
return Column(
children: [ 
Text(docStream.name) //Here is also Anton
RaisedButton(
child: Text("Change Name"),
onPressed: () {
context.read(streamProvider).changeName("Marco"); 
},), //RaisedButton
]
); //Column
}
}

到目前为止,我的理解是,riverpod允许您获取提供者的状态,这基本上是一个值(?(,这就是为什么只在您想要访问其数据的任何Widget中查看它就足够了。没有必要再让Widgets在应用程序中四处传播了(只是代表我的情况(。

下面是我认为正确的解决方案。

我给供应商打了多少电话也无关紧要。它总是相同的实例。对我来说,这意味着doc和doc2是相同的。我希望这能让事情变得更清楚。

class ParentWidget extends ConsumerWidget{
Widget build(BuildContext context, ScopedReader watch){
Stream<DocumentSnapshot> doc = watch(streamProvider.stream);
return Container(
child: Column(
children: [ 
Text(doc.name), //Lets say the name is Anton,
RaisedButton(
child: Text(" road to CustomerPage"),
onPressed:(){
Navigator.of(context).pushNamed(RouteGenerator.customerPage);
},), //RaisedButton
],), //Column
);  //Container
}
}
class CustomerPage extends ConsumerWidget{
Widget build(BuildContext context, ScopedReader watch){
Stream<DocumentSnapshot> doc2 = watch(streamProvider.stream);
return Column(
children: [ 
Text(doc2.name) //Here is also Anton
RaisedButton(
child: Text("Change Name"),
onPressed: () {
context.read(streamProvider).changeName("Marco"); 
},), //RaisedButton
]
); //Column
}
}

最新更新