Complete the code to create a GoRouter with a single route to HomeScreen.
final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => [1](),
),
],
);The builder returns the widget for the route. Here, it should return HomeScreen().
Complete the code to navigate to '/profile' using GoRouter.
ElevatedButton(
onPressed: () {
context.go([1]);
},
child: Text('Go to Profile'),
);The context.go() method needs the full path string starting with '/'.
Fix the error in the GoRoute path to accept a userId parameter.
GoRoute( path: '/user/[1]', builder: (context, state) { final userId = state.params['userId']!; return UserScreen(userId: userId); }, ),
GoRouter uses colon ':' before the parameter name in the path, like ':userId'.
Fill both blanks to create a nested route for settings inside profile.
GoRoute( path: '/profile', builder: (context, state) => ProfileScreen(), routes: [ GoRoute( path: [1], builder: (context, state) => [2](), ), ], ),
Nested routes use relative paths without a leading slash. The builder returns the nested screen widget.
Fill all three blanks to create a GoRouter with initial location and error page.
final router = GoRouter( initialLocation: [1], routes: [ GoRoute( path: '/', builder: (context, state) => HomeScreen(), ), ], errorBuilder: (context, state) => [2](), debugLogDiagnostics: [3], );
initialLocation sets the start path, errorBuilder shows a widget on errors, and debugLogDiagnostics enables debug logs.