Flutter navigator push in initstate 进入循环
Flutter navigator push in initstate going into the loop
如果主页上缺少数据,我正在尝试重定向到另一个页面,但他进入循环并一直工作
@override
void initState() {
super.initState();
_firestore
.collection('users')
.document(widget.firebaseUser.uid)
.snapshots()
.listen((DocumentSnapshot snapshot) => _onMeUpdate(snapshot));
}
void _onMeUpdate(DocumentSnapshot snapshot) {
if (mounted) {
setState(() {
_gender = snapshot.data['gender'] ?? '';
_match = snapshot.data['match'] ?? '';
Future(() {
if (_gender.isEmpty) {
Navigator.of(context).push(CupertinoPageRoute(
builder: (BuildContext context) => Page1(
firebaseUser: widget.firebaseUser)));
} else {
if (_match.isEmpty) {
Navigator.of(context).push(CupertinoPageRoute(
builder: (BuildContext context) => Page2(
firebaseUser: widget.firebaseUser)));
}
}
});
});
}
}
我正在寻找我的答案,我把答案留给你发生的一切。
当他用 pushReplacement 代替 push 时发生了这种情况
Navigator.of(context).pushReplacement(CupertinoPageRoute(
builder: (BuildContext context) => Page2(
firebaseUser: widget.firebaseUser)));
使用这个:
WidgetsBinding.instance.addPostFrameCallback((_) =>
Navigator.of(context).push(CupertinoPageRoute( builder: (BuildContext context) =>
Page2(firebaseUser: widget.firebaseUser))));
如果主页上缺少数据,我正在尝试重定向到另一个页面,但他进入循环并一直工作
@override
void initState() {
super.initState();
_firestore
.collection('users')
.document(widget.firebaseUser.uid)
.snapshots()
.listen((DocumentSnapshot snapshot) => _onMeUpdate(snapshot));
}
void _onMeUpdate(DocumentSnapshot snapshot) {
if (mounted) {
setState(() {
_gender = snapshot.data['gender'] ?? '';
_match = snapshot.data['match'] ?? '';
Future(() {
if (_gender.isEmpty) {
Navigator.of(context).push(CupertinoPageRoute(
builder: (BuildContext context) => Page1(
firebaseUser: widget.firebaseUser)));
} else {
if (_match.isEmpty) {
Navigator.of(context).push(CupertinoPageRoute(
builder: (BuildContext context) => Page2(
firebaseUser: widget.firebaseUser)));
}
}
});
});
}
}
我正在寻找我的答案,我把答案留给你发生的一切。
当他用 pushReplacement 代替 push 时发生了这种情况
Navigator.of(context).pushReplacement(CupertinoPageRoute(
builder: (BuildContext context) => Page2(
firebaseUser: widget.firebaseUser)));
使用这个:
WidgetsBinding.instance.addPostFrameCallback((_) =>
Navigator.of(context).push(CupertinoPageRoute( builder: (BuildContext context) =>
Page2(firebaseUser: widget.firebaseUser))));