Skip to content

[go_router_builder] Runtime exception when using required extra with a nullable named record field.  #135591

@rubenferreira97

Description

@rubenferreira97

Is there an existing issue for this?

Steps to reproduce

Run the code sample example (tweaked the extra_example to reproduce the bug).

Expected results

Check the generated code:

  static RequiredExtraRoute _fromState(GoRouterState state) =>
      RequiredExtraRoute(
        $extra: state.extra as List<({int id, String name})>,
      );

Should be:

  static RequiredExtraRoute _fromState(GoRouterState state) =>
      RequiredExtraRoute(
        $extra: state.extra as List<({int id, String? name})>,
      );

Actual results

When try to call .go the following runtime exception appears:

════════ Exception caught by foundation library ════════════════════════════════
type 'List<({int id, String? name})>' is not a subtype of type 'List<({int id, String name})>' in type cast
═════════════════════════════════════════════════════════════════

Code sample

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

part 'extra_example.g.dart';

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

final GoRouter _router = GoRouter(
  routes: $appRoutes,
  initialLocation: '/splash',
);

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: _router,
    );
  }
}

typedef Extra = List<({int id, String? name})>;

@TypedGoRoute<RequiredExtraRoute>(path: '/requiredExtra')
class RequiredExtraRoute extends GoRouteData {
  const RequiredExtraRoute({required this.$extra});

  final Extra $extra;

  @override
  Widget build(BuildContext context, GoRouterState state) =>
      RequiredExtraScreen(extra: $extra);
}

class RequiredExtraScreen extends StatelessWidget {
  const RequiredExtraScreen({super.key, required this.extra});

  final Extra extra;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Required Extra')),
      body: Center(child: Text('Extra: $extra')),
    );
  }
}

@TypedGoRoute<OptionalExtraRoute>(path: '/optionalExtra')
class OptionalExtraRoute extends GoRouteData {
  const OptionalExtraRoute({this.$extra});

  final Extra? $extra;

  @override
  Widget build(BuildContext context, GoRouterState state) =>
      OptionalExtraScreen(extra: $extra);
}

class OptionalExtraScreen extends StatelessWidget {
  const OptionalExtraScreen({super.key, this.extra});

  final Extra? extra;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Optional Extra')),
      body: Center(child: Text('Extra: $extra')),
    );
  }
}

@TypedGoRoute<SplashRoute>(path: '/splash')
class SplashRoute extends GoRouteData {
  const SplashRoute();

  @override
  Widget build(BuildContext context, GoRouterState state) => const Splash();
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Splash')),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          const Placeholder(),
          ElevatedButton(
            onPressed: () =>
                const RequiredExtraRoute($extra: [(id: 1, name: 'test1')])
                    .go(context),
            child: const Text('Required Extra'),
          ),
          ElevatedButton(
            onPressed: () =>
                const OptionalExtraRoute($extra: [(id: 2, name: 'test2')])
                    .go(context),
            child: const Text('Optional Extra'),
          ),
          ElevatedButton(
            onPressed: () => const OptionalExtraRoute().go(context),
            child: const Text('Optional Extra (null)'),
          ),
        ],
      ),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[√] Flutter (Channel stable, 3.13.5, on Microsoft Windows [Version 10.0.22621.2283], locale pt-PT)
    • Flutter version 3.13.5 on channel stable at C:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 12fccda598 (8 days ago), 2023-09-19 13:56:11 -0700
    • Engine revision bd986c5ed2
    • Dart version 3.1.2
    • DevTools version 2.25.0

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0-rc2)
    • Android SDK at C:\Users\Eu\AppData\Local\Android\sdk
    • Platform android-34, build-tools 34.0.0-rc2
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • CHROME_EXECUTABLE = C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe

[X] Visual Studio - develop Windows apps
    X 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 2022.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 17.0.6+0-b2043.56-10027231)

[√] VS Code (version 1.82.2)
    • VS Code at C:\Users\Eu\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.72.0

[√] Connected device (4 available)
    • Redmi Note 9S (mobile) • 14f27be • android-arm64  • Android 12 (API 31)
    • Windows (desktop)      • windows • windows-x64    • Microsoft Windows [Version 10.0.22621.2283]
    • Chrome (web)           • chrome  • web-javascript • unknown
    • Edge (web)             • edge    • web-javascript • Microsoft Edge 117.0.2045.43

[√] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1High-priority issues at the top of the work listfound in release: 3.13Found to occur in 3.13found in release: 3.15Found to occur in 3.15has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: go_router_builderThe go_router_builder packagepackageflutter/packages repository. See also p: labels.r: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions