[英]Flutter How to pass single data from Stream to another Screen and also got updated
I was making an app with a streambuilder that listen to firestore snapshot, and creating a list of item from it, and when i click one of item it would navigate me to new screen with the detail of the item and i could also modify some info of the item there, the problem arise when i tried to update the info of item in firestore, the first screen with streambuilder got rebuild with updated data while the detail page didnt get updated, the flow is more or less like this:我正在制作一个带有监听firestore快照的streambuilder的应用程序,并从中创建一个项目列表,当我单击其中一个项目时,它会将我导航到包含项目详细信息的新屏幕,我还可以修改一些信息那里的项目,当我尝试更新firestore中项目的信息时出现问题,streambuilder的第一个屏幕使用更新的数据重建,而详细信息页面没有更新,流程或多或少是这样的:
and here is my simplified code:这是我的简化代码:
First Screen where i navigate and pass the asyncsnapshot and index to detail screen第一个屏幕,我在其中导航并将异步快照和索引传递到详细屏幕
class _SalesOrderPenyediaState extends State<SalesOrderPenyedia> {
Widget itemTile(context, SalesOrder element, AsyncSnapshot orderSnap, int index) {
//NAVIGATE TO DETAIL SCREEN
GestureDetector(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => StreamBuilder<List<SalesOrder>>(
stream: orderStreams,
builder: (context, snapshot) {
return SalesOrderDetailPenyedia(
streamOrder: orderSnap,
index: index,
);
}
),
fullscreenDialog: true,
);}
child : Container()}
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder(
stream: orderStreams,
builder: (context, AsyncSnapshot<List<SalesOrder>> snapshot) {
ListView(
children: snapshot.data
.asMap()
.map((index, item) => MapEntry(
index, itemTile(context, item, snapshot, index)))
.values
.toList())
}
}),
),
],
),
Detail Page详情页
class SalesOrderDetailPenyedia extends StatefulWidget {
AsyncSnapshot<List<SalesOrder>> streamOrder;
int index;
SalesOrderDetailPenyedia({ this.index, this.streamOrder});
@override
State<StatefulWidget> createState() => SalesOrderDetailState();
}
class SalesOrderDetailState extends State<SalesOrderDetailPenyedia>{
SalesOrder salesOrder;
OrderServices orderServices = OrderServices();
@override
Widget build(BuildContext context) {
salesOrder = widget.streamOrder.data[widget.index];
return Scaffold(
body : Column()//where i show and update my data
)
}
I am new to flutter and been stuck to this for a few day, any advice will be really appreciated我是 flutter 的新手,并且坚持了几天,任何建议都将不胜感激
I don't see exactly the problem but passing down the asyncSnapshot
to the details page is not very useful as it may lose the reference to the first streamBuilder
builder function it came from.我没有看到确切的问题,但是将
asyncSnapshot
传递到详细信息页面并不是很有用,因为它可能会丢失对它来自的第一个streamBuilder
构建器 function 的引用。
So the best way would be to create a stream (best it would be a BehaviorSubject
from the rxDart
lib) and then add to it with the data you want to pass and then pass that stream to the Details page.因此,最好的方法是创建一个 stream(最好是
rxDart
库中的BehaviorSubject
),然后将要传递的数据添加到其中,然后将 stream 传递到详细信息页面。
Once the update finishes, the firebaseResponse should either return the new data or u manually refresh it and at that time pass it down to the Details widget via the same stream.更新完成后,firebaseResponse 应该返回新数据或手动刷新它,然后通过相同的 stream 将其传递给 Details 小部件。
i can see a point in trying to pass down the same stream to the details page and save on request times (if u have the data necessary to show the items list already) but it is very messy in my opinion, u probably should take another approach, the one i mentioned or an other much simpler one.我可以看到尝试将相同的 stream 传递到详细信息页面并保存请求时间(如果您已经拥有显示项目列表所需的数据)但在我看来它非常混乱,您可能应该再拿一个方法,我提到的一种或另一种更简单的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.