【问题标题】:Flutter firestore listen is interrupting by previous page firestore listenFlutter firestore listen 被上一页 firestore listen 打断
【发布时间】:2019-02-26 12:10:14
【问题描述】:

我正在使用 Firestore 开发应用程序。在我的应用程序主屏幕中,我在initState() 中调用firestore 侦听器。当我进入下一个屏幕(屏幕 B)然后是上一个屏幕(主屏幕)initState() 功能 在屏幕 B 中执行和中断侦听器。我没有在屏幕 B 中调用主屏幕 initState()。 为什么会显示? 如下代码所示。

   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 的代码

    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时输出来了

屏幕 B 初始化状态 loadSecondData 开始 在 MainScreen 的 _loadData firebase fn 中输入

loadSecondData() 正在停止。为什么上一页侦听器在新页面中加载。 我试过StreamBuilder 但不是loadSecondData() 没有中断。

【问题讨论】:

  • 当您调用snapshots().listen(...) 时,您开始侦听来自集合的数据流。当您离开第一个屏幕时,您需要通过取消订阅来停止收听该流。见stackoverflow.com/questions/51918047/…
  • 我用过'streamSub.cancel();'但它并没有停止。我在 dispose() 方法中添加了它。不工作。
  • 你检查是否调用了dispose方法?可能会有更好的生命周期事件来执行此操作,例如 deactivate,尽管我承认我自己从未这样做过,并且很难为这些方法解析 Flutter 文档:docs.flutter.io/flutter/widgets/State/dispose.html

标签: firebase dart flutter google-cloud-firestore


【解决方案1】:

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

loadData()loadSecondData() 的代码相同。你应该改变其中之一。

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

【讨论】:

  • 我已更改代码。我正在使用两个不同的集合。不一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-04
  • 2013-08-09
  • 2019-01-13
  • 2021-06-24
  • 2013-04-30
  • 1970-01-01
  • 2019-03-20
相关资源
最近更新 更多