PUSH 事件在 Flutter 中发生两次是正常的吗?
It's normal that PUSH events occurs ever twice in Flutter?
我试图很好地理解 Flutter 生命周期,但我不确定像 PUSH a view 这样的事件发生两次是否正常。
我正在搜索仅在显示视图时发生一次的事件。例如:
ViewA 显示 eventImSearchingOn 仅发生一次。
ViewA打开ViewB,ViewA停用。
ViewB 返回到 ViewA 并且 eventImSearchingOn 只发生一次。
我试过的代码日志:
InitState SplashScreenUI
ChangeDependencies SplashScreenUI
PUSH SplashScreenUI
InitState SplashScreenUI
ChangeDependencies SplashScreenUI
PUSH SplashScreenUI
主要Class
@override
Widget build(BuildContext context) {
return StoreProvider<AppState>(
store: widget.store,
child: MaterialApp(
...
....
initialRoute: '/SplashScreen',
home: SplashScreenUI(),
navigatorKey: Keys.navKey,
navigatorObservers: [Keys.routeObserver],
routes: <String, WidgetBuilder>{
'/Home': (BuildContext context) => HomePageUI(),
'/Login': (BuildContext context) => LoginPageUI(),
'/Routine': (BuildContext context) => RoutineUI(),
'/Notifications': (BuildContext context) => NotificationsUI(),
'/Chat': (BuildContext context) => ChatUI(),
'/Profile': (BuildContext context) => ProfileUI(),
'/ProfileForm': (BuildContext context) => ProfileFormUI(),
'/Interview': (BuildContext context) => InterviewUI(),
'/BodyCheck': (BuildContext context) => BodyCheckUI(),
'/SplashScreen': (BuildContext context) => SplashScreenUI(),
启动画面Class
class _SplashScreenUIState extends State<SplashScreenUI> with RouteAware{
_SplashScreenUIState();
@override
void initState() {
print("InitState SplashScreenUI");
super.initState();
store.dispatch(CheckLogIn());
}
@override
void didChangeDependencies() {
print("ChangeDependencies SplashScreenUI");
super.didChangeDependencies();
Keys.routeObserver.subscribe(this, ModalRoute.of(context));
}
@override
void deactivate() {
print("Deactivate SplashScreenUI");
super.deactivate();
}
@override
void dispose() {
print("Dispose SplashScreenUI");
Keys.routeObserver.unsubscribe(this);
super.dispose();
}
@override
void didPush() {
print("PUSH SplashScreenUI");
}
@override
void didPopNext() {
print("POP SplashScreenUI");
}
对不起,
问题在那里:
initialRoute: '/SplashScreen',
home: SplashScreenUI(),
双重声明导致 SplashScreen 被调用两次。
改成这样,问题解决
home: SplashScreenUI(),
我试图很好地理解 Flutter 生命周期,但我不确定像 PUSH a view 这样的事件发生两次是否正常。 我正在搜索仅在显示视图时发生一次的事件。例如: ViewA 显示 eventImSearchingOn 仅发生一次。 ViewA打开ViewB,ViewA停用。 ViewB 返回到 ViewA 并且 eventImSearchingOn 只发生一次。
我试过的代码日志:
InitState SplashScreenUI
ChangeDependencies SplashScreenUI
PUSH SplashScreenUI
InitState SplashScreenUI
ChangeDependencies SplashScreenUI
PUSH SplashScreenUI
主要Class
@override
Widget build(BuildContext context) {
return StoreProvider<AppState>(
store: widget.store,
child: MaterialApp(
...
....
initialRoute: '/SplashScreen',
home: SplashScreenUI(),
navigatorKey: Keys.navKey,
navigatorObservers: [Keys.routeObserver],
routes: <String, WidgetBuilder>{
'/Home': (BuildContext context) => HomePageUI(),
'/Login': (BuildContext context) => LoginPageUI(),
'/Routine': (BuildContext context) => RoutineUI(),
'/Notifications': (BuildContext context) => NotificationsUI(),
'/Chat': (BuildContext context) => ChatUI(),
'/Profile': (BuildContext context) => ProfileUI(),
'/ProfileForm': (BuildContext context) => ProfileFormUI(),
'/Interview': (BuildContext context) => InterviewUI(),
'/BodyCheck': (BuildContext context) => BodyCheckUI(),
'/SplashScreen': (BuildContext context) => SplashScreenUI(),
启动画面Class
class _SplashScreenUIState extends State<SplashScreenUI> with RouteAware{
_SplashScreenUIState();
@override
void initState() {
print("InitState SplashScreenUI");
super.initState();
store.dispatch(CheckLogIn());
}
@override
void didChangeDependencies() {
print("ChangeDependencies SplashScreenUI");
super.didChangeDependencies();
Keys.routeObserver.subscribe(this, ModalRoute.of(context));
}
@override
void deactivate() {
print("Deactivate SplashScreenUI");
super.deactivate();
}
@override
void dispose() {
print("Dispose SplashScreenUI");
Keys.routeObserver.unsubscribe(this);
super.dispose();
}
@override
void didPush() {
print("PUSH SplashScreenUI");
}
@override
void didPopNext() {
print("POP SplashScreenUI");
}
对不起, 问题在那里:
initialRoute: '/SplashScreen',
home: SplashScreenUI(),
双重声明导致 SplashScreen 被调用两次。 改成这样,问题解决
home: SplashScreenUI(),