I am trying to navigate to logIn Screen after logout. After logout, the screen is able to navigate but it throws an error:
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.
Code for Navigation Sub List: It is a stateless 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();
}),
),
Code of 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());
}
It's working on Navigator.of(context).pushNamed('routName'), but I can't use it because of navigate back to homeScreen without Login.