Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vware opened this issue Mar 5, 2025 · 3 comments · May be fixed by flutter/packages#8889
Open

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

vware opened this issue Mar 5, 2025 · 3 comments · May be fixed by flutter/packages#8889
Labels
p: go_router_builder The go_router_builder package P2 Important issues not at the top of the work list package flutter/packages repository. See also p: labels. team-go_router Owned by Go Router team triaged-go_router Triaged by Go Router team

Comments

@vware
Copy link

vware commented Mar 5, 2025

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
@darshankawar darshankawar added in triage Presently being triaged by the triage team package flutter/packages repository. See also p: labels. p: go_router_builder The go_router_builder package team-go_router Owned by Go Router team and removed in triage Presently being triaged by the triage team labels Mar 6, 2025
@CaoGiaHieu-dev
Copy link

you should ignore lint from genarete file

analyzer:
  exclude:
    - "**.g.dart"

@vware
Copy link
Author

vware commented Mar 8, 2025

that's not a solution to a regression, though. the builder should emit correct code

@arhamcode
Copy link

same here, The '!' will have no effect because the receiver can't be null.
Try removing the '!' operator.

@chunhtai chunhtai added P2 Important issues not at the top of the work list triaged-go_router Triaged by Go Router team labels Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p: go_router_builder The go_router_builder package P2 Important issues not at the top of the work list package flutter/packages repository. See also p: labels. team-go_router Owned by Go Router team triaged-go_router Triaged by Go Router team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants