Cubit - Flutter 错误 - “查找停用的小部件的祖先是不安全的”

问题描述

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

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.

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

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();
              }),),

登录Cubit代码

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'),但我无法使用它,因为没有登录就导航回主屏幕。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)