【发布时间】:2020-06-19 16:54:38
【问题描述】:
我正在尝试在特定持续时间后导航到某个页面。但是,每当我运行该应用程序时,我都会收到一条错误消息:At this point the state of the widget's element tree is no longer stable. To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling inheritFromWidgetOfExactType() in the widget's didChangeDependencies() method.
我的代码如下所示:
@override
void initState() {
super.initState();
googleSignIn.signInSilently(suppressErrors: false)
.then((account) async {
if (account != null) {
List<Post> posts = await initTimelinePosts(account.id);
List<String> followingList = await configureTimelineForNoPost(
account.id
);
currentUser =
User.fromDocument(await usersRef.document(account.id).get());
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => Home(
posts: posts,
followingList: followingList,
savedState: widget.savedState
)
)
);
} else {
Navigator.pushReplacement(
context,
MaterialPageRoute(
settings: RouteSettings(name: Auth.route),
builder: (context) => Auth(savedState: widget.savedState)
)
);
}
}).catchError((_) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
String userId = prefs.getString('userId');
if (userId != null && userId.isNotEmpty) {
DocumentSnapshot doc = await usersRef.document(userId).get();
currentUser = User.fromDocument(doc);
List<Post> posts = await initTimelinePosts(userId);
List<String> followingList = await configureTimelineForNoPost(userId);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => Home(
posts: posts,
followingList: followingList,
savedState: widget.savedState,
)
)
);
} else {
_timer = new Timer(
Duration(milliseconds: 3000),
() {
Navigator.pushReplacement( // the error message points to this line
context,
MaterialPageRoute(
settings: RouteSettings(name: Auth.route),
builder: (context) => Auth(savedState: widget.savedState)
)
);
}
);
}
});
}
我希望应用程序能够正常运行,但事实并非如此。我该如何解决这个问题?
注意:我真的不知道该写什么了,所以一直抱怨我需要添加更多细节。我相信我已经对描述、标题和代码提供了足够的解释。
【问题讨论】:
标签: flutter