csells / go_router

The purpose of the go_router for Flutter is to use declarative routes to reduce complexity, regardless of the platform you're targeting (mobile, web, desktop), handling deep linking from Android, iOS and the web while still allowing an easy-to-use developer experience.
https://gorouter.dev
441 stars 96 forks source link

Can't use async in redirect: from GoRouter #298

Closed MaximeGer closed 2 years ago

MaximeGer commented 2 years ago

I use the package flutter_secure_storage to store a jwt. And to handle if the jwt is expired I have to run asynchronous code. To redirect to the login page I need to use the redirect: of the GoRouter so it can checks everytime a new screen is displayed.

The problem is that redirect: from GoRouter wants a String? and not a Future<String?> so I can't use async and await keywords otherwise I'll get my screen full of red underlined text. I tried using a ".then" callback but without success for the moment cause it doesn't await the end of the callback.

csells commented 2 years ago

The redirect function itself can't return an async value but you can deal with async values in a standard way in Flutter. Check out this sample: https://gorouter.dev/async-data