modular icon indicating copy to clipboard operation
modular copied to clipboard

pushNamedAndRemoveUntil not working

Open rrifafauzikomara opened this issue 4 years ago • 27 comments

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 in initState).
  • 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

rrifafauzikomara avatar Dec 05 '21 16:12 rrifafauzikomara

I've had the same problem in my app after update flutter to 2.0

chrislliam avatar Dec 07 '21 02:12 chrislliam

I have the same problem using flutter version 2.8 and flutter_modular version 4.3.0

luciano-cervantes avatar Dec 14 '21 20:12 luciano-cervantes

Any update? @jacobaraujo7

rrifafauzikomara avatar Dec 26 '21 20:12 rrifafauzikomara

i got the same issue

hlakyawhtay avatar Jan 14 '22 04:01 hlakyawhtay

Any update? @jacobaraujo7

pedroculque avatar Feb 24 '22 18:02 pedroculque

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:

  1. The implementation of pushNamedAndRemoveUntil consists in a popUntil call followed by a pushNamed call. (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);
}
  1. The implementation of popUntil has a guard clause that makes possible to bypass the predicate and 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

philipguedes avatar Feb 24 '22 20:02 philipguedes

any update, @jacobaraujo7??

diogoroos avatar Mar 10 '22 19:03 diogoroos

Guys, until then you can do:

while (Modular.to.canPop()) {
   Modular.to.pop();
}
Modular.to.pushNamed('/home');

diogoroos avatar Mar 10 '22 19:03 diogoroos

Can you help us @jacobaraujo7?

DEVigodE avatar Mar 14 '22 23:03 DEVigodE

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(

DEVigodE avatar Mar 23 '22 03:03 DEVigodE

Does this problem still persist? If so, could you provide us with an example hosted on github for us to evaluate.

jacobaraujo7 avatar May 06 '22 13:05 jacobaraujo7

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

DEVigodE avatar May 06 '22 19:05 DEVigodE

Same problem here.

allanwolski-openco avatar May 06 '22 20:05 allanwolski-openco

Is there any problem using Modular.navigate? It works by removing all previous routes.

jacobaraujo7 avatar May 09 '22 16:05 jacobaraujo7

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.

jacobaraujo7 avatar May 09 '22 16:05 jacobaraujo7

I couldn't hahaha, do you have an example project, please?

DEVigodE avatar May 09 '22 19:05 DEVigodE

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

jacobaraujo7 avatar May 09 '22 20:05 jacobaraujo7

I have the same problem using flutter version 3.5 and flutter_modular version 5.0.2

lyf571321556 avatar Jul 25 '22 09:07 lyf571321556

Not working in flutter_modular version 5.0.3

dzalif avatar Oct 21 '22 07:10 dzalif

After I updated to 5.0.3 it stopped working

weldonsouza avatar Oct 21 '22 15:10 weldonsouza