Flutter firestore listen is interrupting by previous page firestore listen

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-16 17:15:54

问题


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('/'));

    )


    }

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");
        });
    }

The out put is coming when going to ScreenB

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

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


回答1:


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

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");
});


来源:https://stackoverflow.com/questions/54885276/flutter-firestore-listen-is-interrupting-by-previous-page-firestore-listen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!