我正在开发一个与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 initState loadSecondData started Entered in _loadData firebase fn in MainScreen
而loadSecondData()正在停止。
发布于 2019-02-26 20:52:32
看起来您正在从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");
});https://stackoverflow.com/questions/54885276
复制相似问题