Implement a GoRouter path parameter route and access userId in a widget
This tests GoRouter declarative routing and GoRouterState. Define a GoRoute with path '/users/:userId', read state.params['userId'] in the builder, and handle null-safety. A red flag is parsing the URI manually or using ModalRoute.of instead of state.params.
WHAT THIS TESTS: This question checks if you know how to declare parameterized routes in GoRouter and how to extract path parameters from the GoRouterState object. The interviewer wants to see that you understand the difference between path parameters, query parameters, and extra objects, and that you can handle the null-safety implications of state.params being a Map of String to String. It also reveals whether you rely on imperative Flutter routing patterns or embrace GoRouter's declarative builder model.
A GOOD ANSWER COVERS: First, define a GoRoute with a path string that includes a colon prefix for the parameter, such as path /users/:userId. Second, in the builder callback, access the matched value through state.params with the key userId, remembering that the map value is nullable and must be handled with a null-check, bang operator, or default value. Third, note that path parameters are required for the route to match and are essential for dynamic linking and deep linking. Fourth, briefly contrast this with query parameters in state.queryParams and the extra object in state.extra, explaining that extra is not suitable for web or deep links.
COMMON WRONG ANSWERS: A major red flag is trying to extract the parameter manually by parsing context.location or using ModalRoute.of(context).settings, which ignores GoRouter's state object. Another mistake is assuming state.params returns a non-nullable String without checking, which can throw at runtime. Some candidates also confuse path parameters with query parameters and fail to explain why you would choose one over the other. Finally, recommending state.extra as a primary data-passing mechanism for web apps is incorrect because extra is lost on browser back navigation and cannot be used for dynamic linking.
LIKELY FOLLOW-UPS: The interviewer may ask how you would validate the userId before building the widget, perhaps by checking if it exists in a data store and redirecting if invalid. They might ask how to handle multiple parameters like /users/:userId/posts/:postId, which works the same way with additional keys in state.params. Another follow-up is how to make a parameter optional, which requires a different route definition or query parameters, since path parameters are mandatory for a match. You could also be asked about typed routing with go_router_builder and how code generation avoids stringly-typed params.
ONE CONCRETE EXAMPLE: Suppose you have a UserDetailsScreen that needs a user identifier. You would write GoRoute with path /users/:userId and builder callback that reads final userId = state.params['userId']; if userId is null return a NotFoundScreen; otherwise return UserDetailsScreen with that userId. If you navigate with context.go('/users/42'), the builder receives state.params['userId'] equal to 42. For a search page, you might prefer query parameters: context.go('/search?query=dart') and read state.queryParams['query'], which can be null. If you passed the user object directly via extra, it would work for app-bar back buttons but break on web refresh, so you should prefer path parameters for anything that needs a stable URL.
Read the original → docs.page
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.