我正在使用Firestore开发一个应用程序。在我的应用主屏幕上,我正在initState()
中调用Firestore监听器。当我转到下一个屏幕(屏幕B)时,则前一个屏幕(主屏幕)initState()
功能
在屏幕B中执行和中断侦听器。我没有在屏幕B中调用主屏幕initState()。
为什么显示?
下面显示为代码。
class MainScreen extends StatelessWidget {
MainScreen({Key key}): super(key: key);
//The title want to AUGR SHop
@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 initState loadSecondData已启动 在MainScreen的_loadData firebase fn中输入
loadSecondData()
正在停止。为什么先前的页面侦听器正在新页面中加载。
我尝试了StreamBuilder
,但没有尝试loadSecondData()
,没有中断。
答案 0 :(得分:0)
似乎您正在从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");
});