Skip to content

[go_router_builder] Compile-time exception when using not null List #149675

Closed

Description

@rubenferreira97

Steps to reproduce

Run the code sample example.

Expected results

static ListRoute _fromState(GoRouterState state) => ListRoute(
    ids: (state.uri.queryParametersAll['ids'] ?? const []).map(int.parse).toList(),
);

or

static ListRoute _fromState(GoRouterState state) => ListRoute(
    ids: switch(state.uri.queryParametersAll['ids']) {
        null => const [],
        final ids => ids.map((e) => int.parse(e)).toList(),
    },
  );

Actual results

static ListRoute _fromState(GoRouterState state) => ListRoute(
    ids: state.uri.queryParametersAll['ids']?.map(int.parse).toList(),
);

Code sample

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

part 'main.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,
    );
  }
}

@TypedGoRoute<ListRoute>(path: '/list')
class ListRoute extends GoRouteData {
  const ListRoute({required this.ids});

  final List<int> ids;

  @override
  Widget build(BuildContext context, GoRouterState state) =>
      ListScreen(ids: ids);
}

class ListScreen extends StatelessWidget {
  const ListScreen({super.key, required this.ids});

  final List<int> ids;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('List Example')),
      body: Center(child: Text('Ids: $ids')),
    );
  }
}

@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: Center(
        child: ElevatedButton(
          onPressed: () => const ListRoute(ids: [1,2]).go(context),
          child: const Text('List Example'),
        ),
      ),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[√] Flutter (Channel stable, 3.22.0, on Microsoft Windows [Version 10.0.22631.3672], locale pt-PT)
    • Flutter version 3.22.0 on channel stable at C:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5dcb86f68f (4 weeks ago), 2024-05-09 07:39:20 -0500
    • Engine revision f6344b75dc
    • Dart version 3.4.0
    • DevTools version 2.34.3

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

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

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

[√] Visual Studio - develop Windows apps (Visual Studio Build Tools 2019 16.11.22)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools
    • Visual Studio Build Tools 2019 version 16.11.33130.400
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2023.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.10+0--11572160)

[√] VS Code, 64-bit edition (version 1.89.1)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 3.88.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.22631.3672]
    • Chrome (web)           • chrome  • web-javascript • unknown
    • Edge (web)             • edge    • web-javascript • Microsoft Edge 125.0.2535.79

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

• No issues found!

Activity

  1. darshankawar commented on Jun 5, 2024

    @darshankawar
    Member
    part 'main.g.dart';

    @rubenferreira97 Can you provide this file / code ?

  2. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jun 5, 2024
  3. rubenferreira97 commented on Jun 5, 2024

    @rubenferreira97
    Author

    @darshankawar Here it is:

    // GENERATED CODE - DO NOT MODIFY BY HAND
    
    part of 'main.dart';
    
    // **************************************************************************
    // GoRouterGenerator
    // **************************************************************************
    
    List<RouteBase> get $appRoutes => [
          $listRoute,
          $splashRoute,
        ];
    
    RouteBase get $listRoute => GoRouteData.$route(
          path: '/list',
          factory: $ListRouteExtension._fromState,
        );
    
    extension $ListRouteExtension on ListRoute {
      static ListRoute _fromState(GoRouterState state) => ListRoute(
            ids: state.uri.queryParametersAll['ids']?.map(int.parse).toList(),
          );
    
      String get location => GoRouteData.$location(
            '/list',
            queryParams: {
              'ids': ids.map((e) => e.toString()).toList(),
            },
          );
    
      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);
    }
    
    RouteBase get $splashRoute => GoRouteData.$route(
          path: '/splash',
          factory: $SplashRouteExtension._fromState,
        );
    
    extension $SplashRouteExtension on SplashRoute {
      static SplashRoute _fromState(GoRouterState state) => const SplashRoute();
    
      String get location => GoRouteData.$location(
            '/splash',
          );
    
      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);
    }

    You can also run dart run build_runner build -d with the provided code, assuming build_runner, go_router_builder and go_router are present on pubspec.yaml.

    This produces the following error:

    The argument type 'List<int>?' can't be assigned to the parameter type 'List<int>'. dart[argument_type_not_assignable](https://dart.dev/diagnostics/argument_type_not_assignable)
  4. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jun 5, 2024
  5. darshankawar commented on Jun 5, 2024

    @darshankawar
    Member

    Thanks for the update. I was able to replicate the reported behavior.

    stable, master flutter doctor -v
    [!] Flutter (Channel stable, 3.22.1, on macOS 12.2.1 21D62 darwin-x64, locale
        en-GB)
        • Flutter version 3.22.1 on channel stable at
          /Users/dhs/documents/fluttersdk/flutter
        ! Warning: `flutter` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
          your current Flutter SDK checkout at
          /Users/dhs/documents/fluttersdk/flutter. Consider adding
          /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
        ! Warning: `dart` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
          current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
          Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
          of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision a14f74ff3a (12 days ago), 2024-05-22 11:08:21 -0500
        • Engine revision 55eae6864b
        • Dart version 3.4.1
        • DevTools version 2.34.3
        • If those were intentional, you can disregard the above warnings; however
          it is recommended to use "git" directly to perform update checks and
          upgrades.
    
    [!] Xcode - develop for iOS and macOS (Xcode 12.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        ! Flutter recommends a minimum Xcode version of 13.
          Download the latest version or update via the Mac App Store.
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (5 available)
        • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
        • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
          ios            • iOS 14.4.1 18D61
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
          ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                    •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                                   •
          web-javascript • Google Chrome 98.0.4758.80
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    ! Doctor found issues in 1 category.
    
    [!] Flutter (Channel master, 3.23.0-13.0.pre.100, on macOS 12.2.1 21D62
        darwin-x64, locale en-GB)
        • Flutter version 3.23.0-13.0.pre.100 on channel master at
          /Users/dhs/documents/fluttersdk/flutter
        ! Warning: `flutter` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
          your current Flutter SDK checkout at
          /Users/dhs/documents/fluttersdk/flutter. Consider adding
          /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
        ! Warning: `dart` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
          current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
          Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
          of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 63daac2a51 (4 hours ago), 2024-06-04 21:16:19 -0400
        • Engine revision 11a32d43e3
        • Dart version 3.5.0 (build 3.5.0-222.0.dev)
        • DevTools version 2.36.0
        • If those were intentional, you can disregard the above warnings; however
          it is recommended to use "git" directly to perform update checks and
          upgrades.
    
    [!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
        • Android SDK at /Users/dhs/Library/Android/sdk
        ✗ cmdline-tools component is missing
          Run `path/to/sdkmanager --install "cmdline-tools;latest"`
          See https://developer.android.com/studio/command-line for more details.
        ✗ Android license status unknown.
          Run `flutter doctor --android-licenses` to accept the SDK licenses.
          See https://flutter.dev/docs/get-started/install/macos#android-setup for
          more details.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 13C100
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] IntelliJ IDEA Ultimate Edition (version 2021.3.2)
        • IntelliJ at /Applications/IntelliJ IDEA.app
        • Flutter plugin version 65.1.4
        • Dart plugin version 213.7228
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.29.0
    
    [✓] Connected device (3 available)
        • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios
          • iOS 15.3.1 19D52
        • macOS (desktop)           • macos                                    •
          darwin-x64     • macOS 12.2.1 21D62 darwin-x64
        • Chrome (web)              • chrome                                   •
          web-javascript • Google Chrome 109.0.5414.119
    
    [✓] Network resources
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.
          
    [!] Xcode - develop for iOS and macOS (Xcode 12.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        ! Flutter recommends a minimum Xcode version of 13.
          Download the latest version or update via the Mac App Store.
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (5 available)
        • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
        • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
          ios            • iOS 14.4.1 18D61
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
          ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                    •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                                   •
          web-javascript • Google Chrome 98.0.4758.80
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    ! Doctor found issues in 1 category.
    
    
    
    
  6. added
    packageflutter/packages repository. See also p: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    and removed
    in triagePresently being triaged by the triage team
    on Jun 5, 2024
  7. self-assigned this
    on Jul 18, 2024
  8. added
    ⏳Bot is counting down the days until it unassigns the issue
    on Nov 21, 2024
  9. flutter-triage-bot commented on Nov 21, 2024

    @flutter-triage-bot

    This issue is assigned to @hannah-hyj but has had no recent status updates. Please consider unassigning this issue if it is not going to be addressed in the near future. This allows people to have a clearer picture of what work is actually planned. Thanks!

  10. github-actions commented on Jan 23, 2025

    @github-actions

    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.

  11. locked as resolved and limited conversation to collaborators on Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

⏳Bot is counting down the days until it unassigns the issueP2Important issues not at the top of the work listfound in release: 3.22Found to occur in 3.22found in release: 3.23Found to occur in 3.23has 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: solvedIssue is closed as solved

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions