问题
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