Skip to content

[go_router_builder] Typed route with an int parameter generates a warning #164621

@vware

Description

@vware

Steps to reproduce

  1. Generate code for sample class below
  2. Dart analyze will show "The '!' will have no effect because the receiver can't be null."

This also happens in 2.8.2:

Expected results

Generated code does not show a warning in dart analyzer.

Code emitted should be int.parse(state.pathParameters['id']!) because the parameter is not nullable.

Actual results

Generated code is "int.parse()!", but parse() can never be null.

So dart correctly generates this warning:
The '!' will have no effect because the receiver can't be null.
Try removing the '!' operator.

extension $MyRouteExtension on MyRoute {
  static MyRoute _fromState(GoRouterState state) =>
      MyRoute(int.parse(state.pathParameters['id']!)!);

  String get location => GoRouteData.$location(
    '/product/${Uri.encodeComponent(id.toString())}',
  );

  void go(BuildContext context) => context.go(location);

  Future<T?> push<T>(BuildContext context) => context.push<T>(location);

  void pushReplacement(BuildContext context) =>
      context.pushReplacement(location);

  void replace(BuildContext context) => context.replace(location);
}

Code sample

Code sample
TypedGoRoute<MyRoute>(path: '/product/:id'),

class MyRoute extends GoRouteData {
  final int id;
  const MyRoute (this.id);

  @override
  Widget build(BuildContext context, GoRouterState state) {
    return SomeScreen(productId: id);
  }
}

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.29.0, on Microsoft Windows [Version 10.0.26100.3194], locale de-DE)
[√] Windows Version (11 Pro 64-bit, 24H2, 2009)
[√] Android toolchain - develop for Android devices (Android SDK version 35.0.1)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.12.4)
[√] Android Studio (version 2024.2)
[√] VS Code, 64-bit edition (version 1.97.2)
[√] Connected device (4 available)
[√] Network resources

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listp: go_router_builderThe go_router_builder packagepackageflutter/packages repository. See also p: labels.team-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions