Flutter Firestore监听被上一页Firestore监听打断 [英] Flutter firestore listen is interrupting by previous page firestore listen

查看:63
本文介绍了Flutter Firestore监听被上一页Firestore监听打断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firestore开发一个应用程序。在我的应用程序主屏幕中,我正在 initState()中调用Firestore侦听器。当我转到下一个屏幕(屏幕B)时,上一个屏幕(主屏幕) initState()函数
在屏幕B中执行并中断侦听器。我不是在屏幕B中调用主屏幕initState()。
为什么显示?
下面显示为代码。

I am developing an app with firestore. In my app main screen i am calling firestore listener in initState(). when i am going next screen (Screen B) then the previous screen (main screen) initState() function executing and interrupting listeners in screen B. I am not calling main screen initState() in Screen B. Why it showing ? Below shown as the code.

   class MainScreen extends StatelessWidget {
    MainScreen({Key key}): super(key: key);

    //The title want to My App
    @override
    Widget build(BuildContext context) {
        return new MaterialApp(
            title: 'My App',
            home: MyMainPage(title: 'MyApp',));
    }
    }

    class MyMainPage extends StatefulWidget {
    MyMainPage({Key key, this.title}) : super(key: key);
    final String title;


    @override
    _MyMainPageState createState() => new _MyMainPageState();
    }


    class _MyMainPageState extends State<MyMainPage> with TickerProviderStateMixin {
    _MyMainPageState();

        @override
    void initState(){
        super.initState();

        loadData();

    }

    loadData(){

        widget.authentication.getFirestore().collection("xxx").document("xxx").collection("xxx")
        .snapshots().listen((data){
                debugPrint("Entered in _loadData firebase fn in MainScreen");
        });
    }

        @override
    Widget build(BuildContext context) {
        return new  new Scaffold(
        backgroundColor: Colors.white,
            appBar: new AppBar(
            iconTheme: IconThemeData(
                color: Colors.black, //change font color here
            ),
            ),
            body:new RaisedButton(child:new Text("click),onPressed: () {redirectToScreenB();})

    }

    redirectToScreenB(
            Navigator.push(context,MaterialPageRoute(builder: (context) => ScreenB()));
            //I have used no change found
            //  Navigator.pushAndRemoveUntil(context,MaterialPageRoute(builder: (context) => ScreenB()),ModalRoute.withName('/'));

    )


    }

ScreenB的代码

code for ScreenB

    class ScreenB extends StatefulWidget {
        ScreenBage({Key key, this.title}) : super(key: key);
        final String title;
        @override
        ScreenBPageState createState() => new ScreenBPageState();
    }


    class ScreenBPageState extends State<ScreenB> with TickerProviderStateMixin {
    ScreenBPageState();

        @override
    void initState(){
        super.initState();
        debugPrint("Screen B initState");

        loadSecondData();

    }

        loadSecondData(){

            debugPrint("loadSecondData started");

            widget.authentication.getFirestore().collection("xxx").document("uid")
.collection("data").where("name", isEqualTo:"xxxx")
        .snapshots().listen((data){
                debugPrint("Entered in loadSecondData firebase fn in ScreenB");
        });
    }

进入ScreenB时输出即将到来

The out put is coming when going to ScreenB

屏幕B initState
loadSecondData已开始
在Main_Screen中的_loadData firebase fn中输入

Screen B initState loadSecondData started Entered in _loadData firebase fn in MainScreen

loadSecondData()正在停止。为什么先前的页面侦听器正在新页面中加载。
我尝试了 StreamBuilder ,但没有尝试 loadSecondData()而没有中断。

And the loadSecondData() is stoping . Why the previous page listener is loading in new page. I have tried StreamBuilder but not loadSecondData() without interrupt.

推荐答案

似乎您正在从screenA加载screenB中的相同数据。

It seems that you are loading the same data in screenB from screenA.

您的代码是 loadData() loadSecondData()都相同。您应该更改其中之一。

Your code is the same for both loadData() and loadSecondData(). You should change one of them.

loadData()

{
widget.authentication.getFirestore().collection("xxx").document("xxx").collection("xxx").snapshots().listen((data) {
    debugPrint("Entered in _loadData firebase fn in MainScreen");
});}

loadSecondData()

{
debugPrint("loadSecondData started");
widget.authentication.getFirestore().collection("xxx").document("xxx").collection("xxx").snapshots().listen((data){
debugPrint("Entered in _loadData firebase fn in MainScreen");
});

这篇关于Flutter Firestore监听被上一页Firestore监听打断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆