Aditya Dixit
Aditya Dixit

Reputation: 255

What route predicate means in flutter?

I was using Navigator.pushNamedAndRemoveUntil() method where I came across RoutePredicate.

I can't find a perfect explanation for RoutePredicate anywhere.

Can someone explain this?

Upvotes: 6

Views: 2118

Answers (1)

Mohit Kushwaha
Mohit Kushwaha

Reputation: 1041

A predicate is a function that takes one item as input and returns either true or false based on whether the item satisfies some condition.

A route predicate is a function that removes routes until one with a certain name is found. The Navigator.pushNamedAndRemoveUntil() will stop execution if the predicate returns true, otherwise it will continue to remove routes if the predicate returns false.

To remove all the routes below the pushed route, use a RoutePredicate that always returns false. example-

Navigator.of(context)
    .pushNamedAndRemoveUntil('/login', (Route<dynamic> route) => false);

Here using a RoutePredicate that always returns false (Route<dynamic> route) => false. In this situation it removes all of the routes except for the new /login route which is pushed.

reference - https://api.flutter.dev/flutter/widgets/Navigator/pushNamedAndRemoveUntil.html

Upvotes: 7

Related Questions