Skip to content

StatefulShellRouteData generated as const on extension #161305

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

Closed
on3ss opened this issue Jan 8, 2025 · 6 comments
Closed

StatefulShellRouteData generated as const on extension #161305

on3ss opened this issue Jan 8, 2025 · 6 comments
Assignees
Labels
found in release: 3.27 Found to occur in 3.27 found in release: 3.28 Found to occur in 3.28 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 team-go_router Owned by Go Router team triaged-go_router Triaged by Go Router team

Comments

@on3ss
Copy link

on3ss commented Jan 8, 2025

Steps to reproduce

  1. create a StatefulShellRouteData with multiple branches but single route for each branch
  2. Add a custom scaffold for nesting routes with a bottom navigation bar

Expected results

on the generated file, the extension on the class that extends StatefulShellRouteData must not be returned as const.

Actual results

on the generated file, the extension on the class that extends StatefulShellRouteData returned as const.

Code sample

Code sample
import 'package:academix/features/chat/chat.dart';
import 'package:academix/features/homework/homework.dart';
import 'package:academix/features/my_class/my_class.dart';
import 'package:academix/widgets/scaffold_with_nested_navigation.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:academix/features/home/home.dart';

import '../features/assignments/assignments.dart';

part 'router.g.dart';

@TypedStatefulShellRoute<AppShellRouteData>(
  branches: [
    TypedStatefulShellBranch(
      routes: [TypedGoRoute<HomePageRoute>(path: '/')],
    ),
    TypedStatefulShellBranch(
      routes: [TypedGoRoute<MyClassPageRoute>(path: '/my-class')],
    ),
    TypedStatefulShellBranch(
      routes: [TypedGoRoute<HomeworkPageRoute>(path: '/homework')],
    ),
    TypedStatefulShellBranch(
      routes: [TypedGoRoute<AssignmentsPageRoute>(path: '/assignments')],
    ),
    TypedStatefulShellBranch(
      routes: [TypedGoRoute<ChatPageRoute>(path: '/chat')],
    ),
  ],
)
class AppShellRouteData extends StatefulShellRouteData {
  @override
  Widget builder(
    BuildContext context,
    GoRouterState state,
    StatefulNavigationShell navigationShell,
  ) =>
      ScaffoldWithNestedNavigation(navigationShell: navigationShell);
}

@immutable
class HomePageRoute extends GoRouteData {
  @override
  Widget build(BuildContext context, GoRouterState state) {
    return const MyHomePage();
  }
}

@immutable
class MyClassPageRoute extends GoRouteData {
  @override
  Widget build(BuildContext context, GoRouterState state) {
    return const MyClassPage();
  }
}

@immutable
class AssignmentsPageRoute extends GoRouteData {
  @override
  Widget build(BuildContext context, GoRouterState state) {
    return const AssignmentsPage();
  }
}

@immutable
class HomeworkPageRoute extends GoRouteData {
  @override
  Widget build(BuildContext context, GoRouterState state) {
    return const HomeworkPage();
  }
}

@immutable
class ChatPageRoute extends GoRouteData {
  @override
  Widget build(BuildContext context, GoRouterState state) {
    return const ChatPage();
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
lib/router/router.g.dart:61:13: Error: Cannot invoke a non-'const' constructor where a const expression is expected.
Try using a constructor or factory that is 'const'.
      const AppShellRouteData();

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.27.1, on Microsoft Windows [Version 10.0.26100.2605], locale en-IN)
[✓] Windows Version (Installed version of Windows is version 10 or higher)
[!] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    ! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
[✗] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[✗] Visual Studio - develop Windows apps
    ✗ Visual Studio not installed; this is necessary to develop Windows apps.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components
[✓] Android Studio (version 2024.2)
[✓] Connected device (3 available)
[✓] Network resources

! Doctor found issues in 3 categories.
@on3ss
Copy link
Author

on3ss commented Jan 8, 2025

The generated code:

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'router.dart';

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

List<RouteBase> get $appRoutes => [
      $appShellRouteData,
    ];

RouteBase get $appShellRouteData => StatefulShellRouteData.$route(
      factory: $AppShellRouteDataExtension._fromState,
      branches: [
        StatefulShellBranchData.$branch(
          routes: [
            GoRouteData.$route(
              path: '/',
              factory: $HomePageRouteExtension._fromState,
            ),
          ],
        ),
        StatefulShellBranchData.$branch(
          routes: [
            GoRouteData.$route(
              path: '/my-class',
              factory: $MyClassPageRouteExtension._fromState,
            ),
          ],
        ),
        StatefulShellBranchData.$branch(
          routes: [
            GoRouteData.$route(
              path: '/homework',
              factory: $HomeworkPageRouteExtension._fromState,
            ),
          ],
        ),
        StatefulShellBranchData.$branch(
          routes: [
            GoRouteData.$route(
              path: '/assignments',
              factory: $AssignmentsPageRouteExtension._fromState,
            ),
          ],
        ),
        StatefulShellBranchData.$branch(
          routes: [
            GoRouteData.$route(
              path: '/chat',
              factory: $ChatPageRouteExtension._fromState,
            ),
          ],
        ),
      ],
    );

extension $AppShellRouteDataExtension on AppShellRouteData {
  static AppShellRouteData _fromState(GoRouterState state) =>
      const AppShellRouteData();
}

extension $HomePageRouteExtension on HomePageRoute {
  static HomePageRoute _fromState(GoRouterState state) => HomePageRoute();

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

  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);
}

extension $MyClassPageRouteExtension on MyClassPageRoute {
  static MyClassPageRoute _fromState(GoRouterState state) => MyClassPageRoute();

  String get location => GoRouteData.$location(
        '/my-class',
      );

  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);
}

extension $HomeworkPageRouteExtension on HomeworkPageRoute {
  static HomeworkPageRoute _fromState(GoRouterState state) =>
      HomeworkPageRoute();

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

  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);
}

extension $AssignmentsPageRouteExtension on AssignmentsPageRoute {
  static AssignmentsPageRoute _fromState(GoRouterState state) =>
      AssignmentsPageRoute();

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

  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);
}

extension $ChatPageRouteExtension on ChatPageRoute {
  static ChatPageRoute _fromState(GoRouterState state) => ChatPageRoute();

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

  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);
}

@darshankawar darshankawar added the in triage Presently being triaged by the triage team label Jan 9, 2025
@darshankawar
Copy link
Member

Code sample

@on3ss I code sample you shared here can you provide only minimal or complete runnable one ? I see that you have lot of imports there, but try to trim down to absolutely minimum code that still triggers the reported error.

@darshankawar darshankawar added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Jan 9, 2025
@on3ss
Copy link
Author

on3ss commented Jan 9, 2025

Sure. Here is the minimal code required to run:

// router.dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:academix/ui/home/home.dart';

part 'router.g.dart';

@TypedStatefulShellRoute<AppShellRouteData>(
  branches: [
    TypedStatefulShellBranch(
      routes: [TypedGoRoute<HomeScreenRoute>(path: '/')],
    ),
  ],
)
class AppShellRouteData extends StatefulShellRouteData {
  @override
  Widget builder(
    BuildContext context,
    GoRouterState state,
    StatefulNavigationShell navigationShell,
  ) =>
      HomeScreen();
}

@immutable
class HomeScreenRoute extends GoRouteData {
  @override
  Widget build(BuildContext context, GoRouterState state) {
    return const HomeScreen();
  }
}
//home.dart
import 'package:flutter/material.dart';

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: Placeholder());
  }
}

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Jan 9, 2025
@darshankawar
Copy link
Member

Thanks for the update. I was able to replicate the reported behavior using details provided above.

stable : 3.27.1
master : 3.28.0-2.0.pre.38691

@darshankawar darshankawar added package flutter/packages repository. See also p: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on p: go_router_builder The go_router_builder package found in release: 3.27 Found to occur in 3.27 found in release: 3.28 Found to occur in 3.28 team-go_router Owned by Go Router team and removed in triage Presently being triaged by the triage team labels Jan 10, 2025
@on3ss
Copy link
Author

on3ss commented Jan 16, 2025

Can we expect a fix for this with the next update? For those who often use build_runner with their project this can be annoying.

@chunhtai chunhtai added P2 Important issues not at the top of the work list triaged-go_router Triaged by Go Router team labels Jan 23, 2025
auto-submit bot pushed a commit to flutter/packages that referenced this issue Jan 28, 2025
fixes:flutter/flutter#161305 
also fix some typos: statefull* -> stateful*
@darshankawar darshankawar added the r: fixed Issue is closed as already fixed in a newer version label Jan 28, 2025
Copy link

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 Feb 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
found in release: 3.27 Found to occur in 3.27 found in release: 3.28 Found to occur in 3.28 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 team-go_router Owned by Go Router team triaged-go_router Triaged by Go Router team
Projects
None yet
Development

No branches or pull requests

4 participants