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] generated location is not correct in some cases #102710

Closed
leoshusar opened this issue Apr 28, 2022 · 3 comments · Fixed by flutter/packages#1690
Closed

[go_router_builder] generated location is not correct in some cases #102710

leoshusar opened this issue Apr 28, 2022 · 3 comments · Fixed by flutter/packages#1690
Assignees
Labels
found in release: 2.10 Found to occur in 2.10 found in release: 2.13 Found to occur in 2.13 has reproducible steps The issue has been confirmed reproducible and is ready to work on 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. r: fixed Issue is closed as already fixed in a newer version

Comments

@leoshusar
Copy link

When you define TypedGoRoute like this:

@TypedGoRoute<HomeRoute>(
  path: '/families',
  routes: <TypedGoRoute<GoRouteData>>[
    TypedGoRoute<FamilyRoute>(path: ':familyId')
  ],
)

This is generated:

extension $FamilyRouteExtension on FamilyRoute {
  static FamilyRoute _fromState(GoRouterState state) => FamilyRoute(
        state.params['familyId']!,
      );

  String get location => GoRouteData.$location(
        '/families\${Uri.encodeComponent(familyId)}',
      );

  void go(BuildContext buildContext) => buildContext.go(location, extra: this);
}

The location getter is wrong, it should be '/families/${Uri.encodeComponent(familyId)}'.
Code sample is taken from here.

Also if you use child route with only param like this:

@TypedGoRoute<HomeRoute>(
  path: '/',
  routes: <TypedGoRoute<GoRouteData>>[
    TypedGoRoute<FamilyRoute>(
      path: ':fid',
      routes: <TypedGoRoute<GoRouteData>>[
        TypedGoRoute<PersonRoute>(
          path: ':pid',
          routes: <TypedGoRoute<GoRouteData>>[
            TypedGoRoute<PersonDetailsRoute>(path: ':details'),
          ],
        ),
      ],
    )
  ],
)

This also generates incorrect location:

String get location => GoRouteData.$location(
      '/${Uri.encodeComponent(fid)}\${Uri.encodeComponent(pid.toString())}\${Uri.encodeComponent(_$PersonDetailsEnumMap[details]!)}',
    );

should be '/${Uri.encodeComponent(fid)}/${Uri.encodeComponent(pid.toString())}/${Uri.encodeComponent(_$PersonDetailsEnumMap[details]!)}'
Code sample here.

Logs
flutter doctor -v
[√] Flutter (Channel beta, 2.13.0-0.2.pre, on Microsoft Windows [Version 10.0.22000.613], locale en-GB)
    • Flutter version 2.13.0-0.2.pre at C:\Users\husar\AppData\Local\Flutter\SDK\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 8662e22bac (7 days ago), 2022-04-20 08:21:52 -0700
    • Engine revision 24a02fa5ee
    • Dart version 2.17.0 (build 2.17.0-266.5.beta)
    • DevTools version 2.12.2

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at C:\Users\husar\AppData\Local\Android\sdk
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.2.0 Preview 3.0)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Preview
    • Visual Studio Community 2022 version 17.2.32408.312
    • The current Visual Studio installation is a pre-release version. It may not be supported by Flutter yet.
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 4.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 47.1.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] IntelliJ IDEA Community Edition (version 2021.3)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart

[√] VS Code, 64-bit edition (version 1.66.0)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 3.38.1

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.22000.613]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 101.0.4951.41
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 99.0.1150.46

[√] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.
@danagbemava-nc danagbemava-nc added the in triage Presently being triaged by the triage team label Apr 28, 2022
@maheshmnj
Copy link
Member

maheshmnj commented Apr 28, 2022

Hi @leoshusar, thanks for filing the issue. I am able to reproduce the issue on windows the generated getter has a backslash instead of a forward slash on windows, as a result, the path is incorrect also the dollar gets escaped.

  String get location => GoRouteData.$location(
        '/families\${Uri.encodeComponent(familyId)}',
      );

it should be

  String get location => GoRouteData.$location(
        '/families/${Uri.encodeComponent(familyId)}',
      );

Reproducible on both stable and the master channel.

main.g.dart
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'main.dart';

// **************************************************************************
// GoRouterGenerator
// **************************************************************************

List<GoRoute> get $appRoutes => [
      $homeRoute,
    ];

GoRoute get $homeRoute => GoRouteData.$route(
      path: '/families',
      factory: $HomeRouteExtension._fromState,
      routes: [
        GoRouteData.$route(
          path: ':familyId',
          factory: $FamilyRouteExtension._fromState,
        ),
      ],
    );

extension $HomeRouteExtension on HomeRoute {
  static HomeRoute _fromState(GoRouterState state) => const HomeRoute();

  String get location => GoRouteData.$location(
        '/families',
      );

  void go(BuildContext buildContext) => buildContext.go(location, extra: this);
}

extension $FamilyRouteExtension on FamilyRoute {
  static FamilyRoute _fromState(GoRouterState state) => FamilyRoute(
        state.params['familyId']!,
      );

  String get location => GoRouteData.$location(
        '/families\${Uri.encodeComponent(familyId)}',
      );

  void go(BuildContext buildContext) => buildContext.go(location, extra: this);
}
code sample

data.dart in below code sample can be found here

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: public_member_api_docs

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

import 'data.dart';

part 'main.g.dart';

void main() => runApp(App());

class App extends StatelessWidget {
  App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) => MaterialApp.router(
        routeInformationParser: _router.routeInformationParser,
        routerDelegate: _router.routerDelegate,
        title: _appTitle,
      );

  final GoRouter _router = GoRouter(routes: $appRoutes);
}

@TypedGoRoute<HomeRoute>(
  path: '/families',
  routes: <TypedGoRoute<GoRouteData>>[
    TypedGoRoute<FamilyRoute>(path: ':familyId')
  ],
)
class HomeRoute extends GoRouteData {
  const HomeRoute();

  @override
  Widget build(BuildContext context) => const HomeScreen();
}

class FamilyRoute extends GoRouteData {
  const FamilyRoute(this.familyId);

  final String familyId;

  @override
  Widget build(BuildContext context) =>
      FamilyScreen(family: familyById(familyId));
}

class HomeScreen extends StatelessWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(title: const Text(_appTitle)),
        body: ListView(
          children: <Widget>[
            for (final Family family in familyData)
              ListTile(
                title: Text(family.name),
                onTap: () => FamilyRoute(family.id).go(context),
              )
          ],
        ),
      );
}

class FamilyScreen extends StatelessWidget {
  const FamilyScreen({required this.family, Key? key}) : super(key: key);
  final Family family;

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(title: Text(family.name)),
        body: ListView(
          children: <Widget>[
            for (final Person p in family.people)
              ListTile(
                title: Text(p.name),
              ),
          ],
        ),
      );
}

const String _appTitle = 'GoRouter Example: builder';
flutter doctor -v (windows)
[√] Flutter (Channel stable, 2.10.5, on Microsoft Windows [Version 10.0.19044.1645], locale en-US)
    • Flutter version 2.10.5 at C:\flutter_sdk\stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5464c5bac7 (2 days ago), 2022-04-18 09:55:37 -0700
    • Engine revision 57d3bac3dd
    • Dart version 2.16.2
    • DevTools version 2.9.2

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at C:\Users\mahesh\AppData\Local\Android\sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java      
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)    
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.4)      
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.11.31727.386
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2020.3)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)        

[√] VS Code (version 1.64.2)
    • VS Code at C:\Users\mahesh\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.38.1

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19044.1645]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 100.0.4896.127
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 100.0.1185.44

[√] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
[√] Flutter (Channel master, 2.13.0-0.0.pre.712, on Microsoft Windows [Version 10.0.19044.1645], locale en-IN)
    • Flutter version 2.13.0-0.0.pre.712 at C:\flutter_sdk\master
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 8c22f5648a (3 days ago), 2022-04-25 05:34:07 -0400
    • Engine revision 10efa195bf
    • Dart version 2.18.0 (build 2.18.0-44.0.dev)
    • DevTools version 2.12.2

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)    
    • Android SDK at C:\Users\mahesh\AppData\Local\Android\sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822)        
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.4)      
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.11.31727.386
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2021.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822)

[√] VS Code (version 1.64.2)
    • VS Code at C:\Users\mahesh\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.38.1

[√] Connected device (5 available)
    • Redmi K20 Pro (mobile)  • 192.168.1.2:5553 • android-arm64   • Android 11 (API 30)
    • Windows (desktop)       • windows          • windows-x64     • Microsoft Windows [Version 10.0.19044.1645]
    • Windows (UWP) (desktop) • winuwp           • windows-uwp-x64 • 
    • Chrome (web)            • chrome           • web-javascript  • Google Chrome 100.0.4896.127
    • Edge (web)              • edge             • web-javascript  • Microsoft Edge 100.0.1185.50

[√] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

@maheshmnj maheshmnj added p: first party package flutter/packages repository. See also p: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on found in release: 2.10 Found to occur in 2.10 found in release: 2.13 Found to occur in 2.13 p: go_router_builder The go_router_builder package and removed in triage Presently being triaged by the triage team labels Apr 28, 2022
@stuartmorgan stuartmorgan added the P2 Important issues not at the top of the work list label May 4, 2022
@leoshusar
Copy link
Author

@chunhtai I think you misassigned :) I'm not the PR author.

@chunhtai chunhtai assigned moko256 and unassigned leoshusar May 5, 2022
@maheshmnj maheshmnj added the r: fixed Issue is closed as already fixed in a newer version label May 19, 2022
@github-actions
Copy link

github-actions bot commented Jun 2, 2022

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
found in release: 2.10 Found to occur in 2.10 found in release: 2.13 Found to occur in 2.13 has reproducible steps The issue has been confirmed reproducible and is ready to work on 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. r: fixed Issue is closed as already fixed in a newer version
Projects
No open projects
Status: Done
5 participants