Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Resolve your routes
Cristovão Farias
Cristovão Farias

Posted on

     

Resolve your routes

There are times when we want to work with a login feature and need to create different routes based on the user's status, redirecting them to the registration page or even identifying a user with a bonus. In these cases, we often place the code directly on the page and send the user to the appropriate route based on their state or status.

class AuthState {  final bool isAuthenticated;  AuthState(this.isAuthenticated);}void main() {  final authState = AuthState(isAuthenticated: false);  String initialRoute;  if (authState.isAuthenticated) {    initialRoute = '/home';  } else {    initialRoute = '/login';  }  runApp(MyApp(initialRoute: initialRoute));}
Enter fullscreen modeExit fullscreen mode

However, this is not always the best solution because, depending on the number of routes, the logic in the view can become extensive and unwieldy if there is no standard or pattern in place. With this in mind, we can consider a solution to the problem, something that allows for unit testing if necessary.

We will create a simple abstraction using generic types, where we will have a method that returns the route string. The method's return can be an enum of routes or something similar, as long as we have control over it.

abstract class RouteResolver<T> {  String resolveRoute(T states);}
Enter fullscreen modeExit fullscreen mode

With our abstraction created, let's now implement our Resolver by module. This way, we separate the logic from the view, allowing us to inject our resolver as a dependency or, for simpler cases, instantiate it on the page where we need to use it.

class AuthState {  bool isAuthenticated;  AuthState(this.isAuthenticated);}class AuthRouteResolver implements RouteResolver<AuthState> {  @override  String resolveRoute(AuthState states) {    if (states.isAuthenticated) {      return '/home';    } else {      return '/login';    }  }}
Enter fullscreen modeExit fullscreen mode

Instantiating our route resolver.

void main() {  final authState = AuthState(isAuthenticated: true);  final routeResolver = AuthRouteResolver();  final initialRoute = routeResolver.resolveRoute(authState);  runApp(MyApp(initialRoute: initialRoute));}
Enter fullscreen modeExit fullscreen mode

I believe this is a simple solution to a common problem. Of course, there are other ways to work with routes. In this particular case, we are working based on a state, status, or even a class type, hence the importance of using generic types. To better understand the topic discussed here, I recommend reading about clean architecture, clean code, and code refactoring.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Passionate about tech and gaming, I'm a self-taught problem solver seeking to innovate and develop innovative solutions. Let's create something amazing together!
  • Location
    Maceió - Alagoas, Brazil
  • Work
    Mobile Software Engineer at Grupo SBF
  • Joined

More fromCristovão Farias

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp