pushNamedAndRemoveUntil not working
I use Modular.to.pushNamedAndRemoveUntil(routeName, (route) => false); it's not clear all route. I have case below.
- I have 3 screens (A, B, and C).
- I open that screen with flow (A -> B -> C -> A).
- So in Screen C, I use
Modular.to.pushNamedAndRemoveUntil(routeNameA, (route) => false);to clear all route and open Screen A to refresh the content in Screen A (ex: call the function ininitState). - Screen A is opened and also it's called again the function in
initState. But the previous route was not cleared.
And when I see the documentation, we can use Modular.to.navigate(routeNameA). This function clear all route but when opening Screen A, it's not calling initState again.
I remember before null safety it's working because I also use this package in the old project.
2.0.1 -> work
4.2.0 -> not work
I've had the same problem in my app after update flutter to 2.0
I have the same problem using flutter version 2.8 and flutter_modular version 4.3.0
Any update? @jacobaraujo7
i got the same issue
Any update? @jacobaraujo7
TL;DR
pushNamedAndRemoveUntil doesn't handle predicates that always returns false the way its Flutter counterpart does.
A workaround would be:
Modular.to.popUntil((route) => route.isFirst);
Modular.to.pushReplacementNamed(routeNameA);
The Problem
To understand the problem, there are two things to point out first:
- The implementation of
pushNamedAndRemoveUntilconsists in apopUntilcall followed by apushNamedcall. (source)
@override
Future<T?> pushNamedAndRemoveUntil<T extends Object?>(
String newRouteName, bool Function(Route) predicate,
{Object? arguments, bool forRoot = false}) {
popUntil(predicate);
return pushNamed<T>(newRouteName, arguments: arguments, forRoot: forRoot);
}
- The implementation of
popUntilhas a guard clause that makes possible to bypass thepredicateand pop until first route. (source)
@override
void popUntil(bool Function(Route) predicate) {
var isFoundedPages = currentConfiguration?.routes.where((route) {
return predicate(CustomModalRoute(ModularPage(
route: route,
args: ModularArguments.empty(),
flags: ModularFlags())));
});
isFoundedPages ??= [];
if (isFoundedPages.isEmpty) {
navigatorKey.currentState?.popUntil((route) => route.isFirst);
} else {
navigatorKey.currentState?.popUntil(predicate);
}
}
As we can see, pushNamedAndRemoveUntil pass the predicate directly to popUntil.
When popUntil receives a predicate that always return false, any call to predicate(any_arg) will always return false.
Then, in popUntil line 229, isFoundedPages will receive an empty array, since where filters all elements that returns true.
In line 236, isFoundedPages is still []
Finally, in line 237, isFoundedPages.isEmpty returns true, since isFoundedPages is an empty array and therefore popUntil calls navigatorKey.currentState?.popUntil((route) => route.isFirst); instead of navigatorKey.currentState?.popUntil(predicate);.
And that's the root cause of the problem 😄 @jacobaraujo7
any update, @jacobaraujo7??
Guys, until then you can do:
while (Modular.to.canPop()) {
Modular.to.pop();
}
Modular.to.pushNamed('/home');
Can you help us @jacobaraujo7?
Alternativa que eu achei
return WillPopScope(
onWillPop: () async {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('The System Back Button is Deactivated')));
return false;
},
child: Scaffold(
Does this problem still persist? If so, could you provide us with an example hosted on github for us to evaluate.
Hi @jacobaraujo7,
Link de project -> https://github.com/ZIBMBRAZIL/testeModular
Video -> https://youtu.be/7rwoMcrjG_c
https://user-images.githubusercontent.com/13280820/167208067-d7249f23-9588-4cda-b874-9d2a64651b19.mp4
Same problem here.
Is there any problem using Modular.navigate? It works by removing all previous routes.
Hi @jacobaraujo7,
Link de project -> https://github.com/ZIBMBRAZIL/testeModular
Video -> https://youtu.be/7rwoMcrjG_c
2022-05-06.16-47-03.mp4
Please,. check flutter_modular version 5.0.0-dev.15
But, consider using Modular.navigate in this situation.
I couldn't hahaha, do you have an example project, please?
I couldn't hahaha, do you have an example project, please? Please,. check flutter_modular version 5.0.0-dev.15 pushNamedAndRemoveUntil has benn fixed
I have the same problem using flutter version 3.5 and flutter_modular version 5.0.2
Not working in flutter_modular version 5.0.3
After I updated to 5.0.3 it stopped working