1

我试图在注销后导航到登录屏幕。注销后,屏幕可以导航,但会引发错误:

Looking up a deactivated widget's ancestor is unsafe.

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 dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.

导航子列表的代码:它是一个无状态的 Widget-

BlocListener<LoginCubit, LoginState>(
          listenWhen: (previous, current) => current is LogoutSuccess,
          listener: (context, state) {
            Navigator.of(context).pushNamedAndRemoveUntil(
              RouteList.initial,
                (route) => false ,
            );
          },
          child: NavigationListItem(
              title: TranslationConstants.logout.t(context),
              onPressed: () {
                BlocProvider.of<LoginCubit>(context).logout();
              }),
        ),

LoginCubit代码:

class LoginCubit extends Cubit<LoginState> {
final LoginUser loginUser;
final LogoutUser logoutUser;
final LoadingCubit loadingCubit;


LoginCubit({required this.loadingCubit, required this.loginUser, required this.logoutUser}) : 
super(LoginInitial());

void initiateLogin(String username, String password) async {
  loadingCubit.show();
  final Either<AppError, bool> eitherResponse = await loginUser(
    LoginRequestParams(
      userName: username,
      password: password,
    ),
  );

emit(eitherResponse.fold(
      (l) {
    var message = getErrorMessage(l.appErrorType);
    print(message);
    return LoginError(message);
    },
        (r) => LoginSuccess(),
  ));
  loadingCubit.hide();
}

void logout() async {
  await logoutUser(NoParams());
  emit(LogoutSuccess());
}

它正在使用 Navigator.of(context).pushNamed('routName'),但我无法使用它,因为在没有登录的情况下导航回 homeScreen。

4

0 回答 0