简体   繁体   English

Flutter 如何将单个数据从 Stream 传递到另一个屏幕并得到更新

[英]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.

相关问题 如何在FLutter中使用pushNamed将数据从一个屏幕传递到另一个屏幕? - how to pass data from one screen to another screen with pushNamed in FLutter? 如何将 JSON 数据从 gridview 传递到 flutter 中的另一个屏幕 - How to pass JSON data to another screen in flutter from a gridview 我如何从另一个屏幕接收编辑/更新的数据到 Flutter 的主屏幕? - How can i recieve edited/updated data from another screen to the main screen in Flutter? 如何在抖动中将Json数据传递到另一个屏幕 - How to pass Json data to another screen in flutter Flutter 作为 stream 将数据从一个屏幕传递到另一个屏幕 - Flutter passing data from one screen to another as a stream 如何在 Flutter 中将文本小部件数据从一个屏幕传递到另一个屏幕? - How to pass text widget data from one screen to another screen in Flutter? Flutter如何将数据从第三屏传递到第一屏? - How to pass data from the third screen to first screen in Flutter? 如何在颤振中使用 bloc 将数据传递到另一个屏幕 - how to pass data to another screen using bloc in flutter 如何将数据传递给 Flutter/Dart 中另一个屏幕的类? - How to pass data to the class of another screen in Flutter/Dart? 如何将一个屏幕作为参数传递给颤动的另一个屏幕? - How to pass a screen as a parameter to another screen in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM