Flutter remove all routes using auto_route

Issue

In case of error, I want to redirect my users to the same flow again (in order to reset the flow state).

I’m using the auto_route package for navigation, and I can see that router.replace and router.replaceAll do not replace (remove and insert from the stack) the same routes again (it just updates them).

From the official documentation:

// removes last entry in stack and pushs provided route            
// if last entry == provided route page will just be updated
router.replace(const BooksListRoute())

// This's like providing a completely new stack as it rebuilds the stack          
// with the list of passed routes          
// entires might just update if already exist          
router.replaceAll([          
   LoginRoute()          
]);          

How can I "hard reset" replace a route, or stack of routes, using auto_route package?

Solution

Solved.

In order to "hard reset" the same page using auto_route package, Use pushAndPopUntil function, with a predicate set to always false.

That way, all pages will be removed, and the provided stack will be inserted instead (in other words, a full replacement, even for the same page).

Then your page will reinitiate, and all state values will be reset (same as redirecting to a new page).

await _router.pushAndPopUntil(
    const LoginRoute(),
    predicate: (_) => false,
)

Answered By – genericUser

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *