Необработанное исключение: Поиск предка деактивированного виджета небезопасен. На этом этапе состояние дерева элементов виджета больше не является стабильным

#flutter #dart

Вопрос:

У меня в ящике есть кнопка выхода из системы.

Когда я нажимаю выход из системы, это те действия, которые необходимо выполнить:

  1. Показать диалог оповещения
  2. Да нажата да, появится диалоговое окно с предупреждением
  3. Выдвиньте ящик
  4. Показать диалог с предупреждением, довольствуясь «Загрузкой…»
  5. Через 2 секунды главный экран должен появиться и перейти на экран входа

Я сделал код для этого:

     if (index == 5) {
      // Open a dialogue
      final bool isLogout = await GlobalService.showAlert(
          context, 'Exit', 'Do you want to logout?');
      if (isLogout) {
        // The text button closes the dialogue
        final isSignOut = await AuthService.signOut(context);
        if (isSignOut) {
          // Closes the drawer
          Navigator.pop(context);
          // Open loading dialogue
          showDialog(
              context: context,
              builder: (context) =>
                  const CustomDialogue(label: 'Logging out...'));
          Future.delayed(const Duration(seconds: kSeconds), () {
            // Pop the loading dialogue
            Navigator.pop(context);
            // Push replacement to login screen
            Navigator.of(context).popAndPushNamed(LoginScreen.id);
          });
        }
      }
    }
  }

 

Но я получаю небезопасную ошибку:

 E/flutter (12079): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
E/flutter (12079): At this point the state of the widget's element tree is no longer stable.
E/flutter (12079): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.
E/flutter (12079): #0      Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure> (package:flutter/src/widgets/framework.dart:4032:9)
E/flutter (12079): #1      Element._debugCheckStateIsActiveForAncestorLookup (package:flutter/src/widgets/framework.dart:4046:6)
E/flutter (12079): #2      Element.findAncestorStateOfType (package:flutter/src/widgets/framework.dart:4093:12)
E/flutter (12079): #3      Navigator.of (package:flutter/src/widgets/navigator.dart:2736:40)
E/flutter (12079): #4      Navigator.pop (package:flutter/src/widgets/navigator.dart:2620:15)
E/flutter (12079): #5      MyDrawer._drawerSelection.<anonymous closure> (package:uber_driver_clone_app/views/widgets/my_drawer.dart:36:23)
E/flutter (12079): #6      new Future.delayed.<anonymous closure> (dart:async/future.dart:315:39)
E/flutter (12079): #7      _rootRun (dart:async/zone.dart:1420:47)
E/flutter (12079): #8      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter (12079): #9      _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
E/flutter (12079): #10     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
E/flutter (12079): #11     _rootRun (dart:async/zone.dart:1428:13)
E/flutter (12079): #12     _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter (12079): #13     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1260:23)
E/flutter (12079): #14     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter (12079): #15     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395:19)
E/flutter (12079): #16     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426:5)
E/flutter (12079): #17     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
E/flutter (12079):