#flutter #lifecycle
#flutter #жизненный цикл
Вопрос:
Я пытаюсь хорошо понять жизненный цикл Flutter, и я не уверен, что это нормально, что такие события, как PUSH-просмотр, происходят дважды. Я ищу событие, которое происходит только один раз, когда отображается представление. Например: ViewA отображается, eventImSearchingOn происходит только один раз. ViewA открыть ViewB, ViewA деактивирован. ViewB возвращается к ViewA, и eventImSearchingOn выполняется только один раз.
Журнал кода, который я пробовал:
InitState SplashScreenUI
ChangeDependencies SplashScreenUI
PUSH SplashScreenUI
InitState SplashScreenUI
ChangeDependencies SplashScreenUI
PUSH SplashScreenUI
Основной класс
@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(),
Класс SplashScreen
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");
}
Ответ №1:
Извините, проблема в этом:
initialRoute: '/SplashScreen',
home: SplashScreenUI(),
Это двойное объявление приводит к тому, что SplashScreen вызывается дважды.
Измените это, решите проблему.
home: SplashScreenUI(),