Это нормально, что события PUSH происходят во Флаттере дважды? - PullRequest
0 голосов
/ 03 апреля 2019

Я пытаюсь хорошо понять жизненный цикл 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 Ответ

0 голосов
/ 03 апреля 2019

Извините, проблема есть:

initialRoute: '/SplashScreen',
home: SplashScreenUI(),

Это двойное объявление вызывает двойной вызов SplashScreen.Перейдите к этому, решите проблему.

home: SplashScreenUI(),
...