auto_route_library icon indicating copy to clipboard operation
auto_route_library copied to clipboard

This route or it's ancestors must have a path-param with the name id

Open SSebigo opened this issue 1 year ago • 6 comments

So I have this error:

This route or it's ancestors must have a path-param with the name id
package:wine/presentation/typewriter/typewriter_page.dart:20:27
   ╷
20 │     @PathParam('id') this.uid,
   │                           ^^^
   ╵

in this piece of code when trying to generate the router:

class TypewriterPage extends StatelessWidget {
  /// @nodoc
  const TypewriterPage({
    super.key,
    @PathParam('id') this.uid,
    ...
  });

  /// Tree or Branch id.
  final String? uid;

  ...
}

The router goes as follows:

part 'router.gr.dart';

@AdaptiveAutoRouter(
  replaceInRouteName: 'Page,Route',
  routes: <AutoRoute>[
    AutoRoute<BranchPage>(
      page: BranchPage,
      path: '/branch/:id',
    ),
    ...
    AutoRoute<TreePage>(
      page: TreePage,
      path: '/tree/:id',
    ),
    ...
    AutoRoute<TypewriterPage>(
      name: 'TypewriterBranchUID',
      page: TypewriterPage,
      path: '/typewriter/branch/:id',
    ),
    AutoRoute<TypewriterPage>(
      name: 'TypewriterBranchNew',
      page: TypewriterPage,
      path: '/typewriter/branch/new',
    ),
    AutoRoute<TypewriterPage>(
      name: 'TypewriterTreeUID',
      page: TypewriterPage,
      path: '/typewriter/tree/:id',
    ),
    AutoRoute<TypewriterPage>(
      name: 'TypewriterTreeNew',
      page: TypewriterPage,
      path: '/typewriter/tree/new',
    ),
  ],
)

/// @nodoc
class AppRouter extends _$AppRouter {}

I've removed the routes without @ParamPath('id) this.uid from the code sample.

I don't really understand where the problem is, it worked well until I updated to v5.0.1 (from v4.0.1).

SSebigo avatar Sep 01 '22 14:09 SSebigo

@SSebigo problem with this definition

AutoRoute<TypewriterPage>( name: 'TypewriterBranchNew', page: TypewriterPage, path: '/typewriter/branch/new', ),

"TypewriterPage" has path param "id" but your definition doesn't

Raviteja11122 avatar Sep 03 '22 14:09 Raviteja11122

@Raviteja11122 but the argument is nullable, shouldn't the generator ignore it in cases where the ID is not needed ? That's why I create different paths, one where the ID is given and one where it's not.

SSebigo avatar Sep 04 '22 11:09 SSebigo

@SSebigo have you tried query params? maybe it helps.

Raviteja11122 avatar Sep 04 '22 13:09 Raviteja11122

Hello @Raviteja11122 Because nullable/optional path params are not considered a good practice auto_route stopped supporting them a long time ago.
You can use queryParams are @SSebigo suggested or you can simply change the order if the two routes

    AutoRoute<TypewriterPage>(
      name: 'TypewriterBranchNew',
      page: TypewriterPage,
      path: '/typewriter/branch/new',
    ),
   AutoRoute<TypewriterPage>(
      name: 'TypewriterBranchUID',
      page: TypewriterPage,
      path: '/typewriter/branch/:id',
    ),

this way the matcher will match this path first /typewriter/branch/new if you pass anything else other than 'new' the second route will be matched.

What I usually do is this

   RedirectRoute(path: '/typewriter/branch', to: 'typewriter/branch/new'),
   AutoRoute<TypewriterPage>(
      name: 'TypewriterBranchUID',
      page: TypewriterPage,
      path: '/typewriter/branch/:id',
    ),

so when users type '/typewriter/branch' in the browser you can get directed the right route

Milad-Akarie avatar Sep 08 '22 10:09 Milad-Akarie

Thanks for the reply @Milad-Akarie. I think @SSebigo got the solution.

Raviteja11122 avatar Sep 09 '22 05:09 Raviteja11122

@Milad-Akarie Hey, i am not sure if i understand your solution correctly. My implementation of simple page for manage event:

class ManageEventPage extends StatefulWidget {
  const ManageEventPage(
    @PathParam('uuid') this.uuid, {
    Key? key,
  }) : super(key: key);

  final String? uuid;

  @override
  State<ManageEventPage> createState() => _ManageEventPageState();
}

and part in CustomAutoRouter responsible for its route:

@CustomAutoRouter(
  replaceInRouteName: 'Page,Route',
  transitionsBuilder: TransitionsBuilders.noTransition,
  routes: <AutoRoute>[
      AutoRoute(
      page: ManageEventPage,
      path: '/events/event/add',
      name: 'AddEventRoute',
    ),
    AutoRoute(
      page: ManageEventPage,
      path: '/events/event/:uuid',
      name: 'EditEventRoute',
    ),
      ],
)

As a result when i try to generate code i get This route or it's ancestors must have a path-param with the name uuid. The problem is that it is pretty common to have one page responsible for editing and creating events and other. For now the only solution that i can see is to create extra class above the ManageEventPage with or without uuid params which not sound right for me. Is there any other solution for this?

jrolinskifdt avatar Sep 12 '22 13:09 jrolinskifdt

@jrolinskifdt Did you find a solution for that ? I have the same problem and can't find the solution for it.

tstarasinic avatar Oct 27 '22 10:10 tstarasinic

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions

github-actions[bot] avatar Dec 27 '22 08:12 github-actions[bot]