Skip to content

[go_router_builder] Iterable parameters don't support default values #122003

Closed
flutter/packages
#3391

Description

@ValentinVignal

Steps to Reproduce

  1. Execute flutter pub run build_runner build --delete-conflicting-outputs on the code sample (see "Code sample" section below)
  2. The default value for the Iterable parameter is not copied and there are errors in the generated code (see "Logs" section below)

Expected results:

Default parameters should be supported on all the supported types (aka Iterable)

Actual results:

Iterable parameters don't support default values

Code sample

Or you can check out https://github.com/ValentinVignal/flutter_app_stable/tree/go-router-builder/default-value-with-iterable-parameters

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

part 'main.g.dart';

@TypedGoRoute<MyRoute>(path: '/')
class MyRoute extends GoRouteData {
  const MyRoute({
    this.iterable = const [],
  });

  final Iterable<int> iterable;

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

which generates

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'main.dart';

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

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

GoRoute get $myRoute => GoRouteData.$route(
      path: '/',
      factory: $MyRouteExtension._fromState,
    );

extension $MyRouteExtension on MyRoute {
  static MyRoute _fromState(GoRouterState state) => MyRoute(
        iterable: state.queryParametersAll['iterable']?.map(int.parse), // <- The argument type 'Iterable<int>?' can't be assigned to the parameter type 'Iterable<int>'
      );

  String get location => GoRouteData.$location(
        '/',
        queryParams: {
          if (iterable != const [])
            'iterable': iterable?.map((e) => e.toString()).toList(), // <- The receiver can't be null, so the null-aware operator '?.' is unnecessary.
Try replacing the operator '?.' with '.'
        },
      );

  void go(BuildContext context) => context.go(location);

  void push(BuildContext context) => context.push(location);

  void pushReplacement(BuildContext context) =>
      context.pushReplacement(location);
}
Logs

No log

Analyzing flutter_app_stable...                                         

  error • The argument type 'Iterable<int>?' can't be assigned to the parameter type 'Iterable<int>' • lib/main.g.dart:20:19 • argument_type_not_assignable
   info • Equality operator `==` invocation with references of unrelated types • lib/main.g.dart:26:15 • unrelated_type_equality_checks
warning • The receiver can't be null, so the null-aware operator '?.' is unnecessary • lib/main.g.dart:27:33 • invalid_null_aware_operator

3 issues found. (ran in 4.1s)

Activity

  1. danagbemava-nc commented on Mar 6, 2023

    @danagbemava-nc
    Member

    Reproducible using the code sample and the steps outlined above.

    flutter doctor -v
    [✓] Flutter (Channel stable, 3.7.6, on macOS 13.2.1 22D68 darwin-arm64, locale en-GB)
        • Flutter version 3.7.6 on channel stable at /Users/nexus/dev/sdks/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 12cb4eb7a0 (5 days ago), 2023-03-01 10:29:26 -0800
        • Engine revision ada363ee93
        • Dart version 2.19.3
        • DevTools version 2.20.1
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
        • Android SDK at /Users/nexus/Library/Android/sdk
        • Platform android-33, build-tools 33.0.0
        • Java binary at: /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/221.6008.13.2211.9477386/Android Studio.app/Contents/jbr/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.2)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14C18
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2022.1)
        • Android Studio at /Applications/Android Studio.app/Contents
        • 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.15+0-b2043.56-8887301)
    
    [✓] Android Studio (version 2022.1)
        • Android Studio at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/221.6008.13.2211.9477386/Android Studio.app/Contents
        • 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.15+0-b2043.56-8887301)
    
    [✓] IntelliJ IDEA Community Edition (version 2022.3.2)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 72.1.4
        • Dart plugin version 223.8617.8
    
    [✓] VS Code (version 1.76.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.60.0
    
    [✓] Connected device (3 available)
        • M2007J20CG (mobile) • adb-5dd3be00-17AYzd._adb-tls-connect._tcp. • android-arm64  • Android 12 (API 31)
        • macOS (desktop)     • macos                                      • darwin-arm64   • macOS 13.2.1 22D68 darwin-arm64
        • Chrome (web)        • chrome                                     • web-javascript • Google Chrome 110.0.5481.177
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    • No issues found!
    
    [!] Flutter (Channel master, 3.9.0-1.0.pre.50, on macOS 13.2.1 22D68 darwin-arm64, locale en-GB)
        • Flutter version 3.9.0-1.0.pre.50 on channel master at /Users/nexus/dev/sdks/flutters
        ! Warning: `flutter` on your path resolves to /Users/nexus/dev/sdks/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutters. Consider adding /Users/nexus/dev/sdks/flutters/bin to the front of your path.
        ! Warning: `dart` on your path resolves to /Users/nexus/dev/sdks/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutters. Consider adding /Users/nexus/dev/sdks/flutters/bin to the front of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 6afaeab23a (5 hours ago), 2023-03-06 01:42:24 -0500
        • Engine revision b4297f3cc8
        • Dart version 3.0.0 (build 3.0.0-290.0.dev)
        • DevTools version 2.22.2
        • 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 33.0.0)
        • Android SDK at /Users/nexus/Library/Android/sdk
        • Platform android-33, build-tools 33.0.0
        • Java binary at: /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/221.6008.13.2211.9477386/Android Studio.app/Contents/jbr/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.2)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14C18
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2022.1)
        • Android Studio at /Applications/Android Studio.app/Contents
        • 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.15+0-b2043.56-8887301)
    
    [✓] Android Studio (version 2022.1)
        • Android Studio at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/221.6008.13.2211.9477386/Android Studio.app/Contents
        • 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.15+0-b2043.56-8887301)
    
    [✓] IntelliJ IDEA Community Edition (version 2022.3.2)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 72.1.4
        • Dart plugin version 223.8617.8
    
    [✓] VS Code (version 1.76.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.60.0
    
    [✓] Connected device (3 available)
        • M2007J20CG (mobile) • adb-5dd3be00-17AYzd._adb-tls-connect._tcp. • android-arm64  • Android 12 (API 31)
        • macOS (desktop)     • macos                                      • darwin-arm64   • macOS 13.2.1 22D68 darwin-arm64
        • Chrome (web)        • chrome                                     • web-javascript • Google Chrome 110.0.5481.177
    
    [✓] Network resources
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.
    
    
  2. 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 Mar 6, 2023
  3. added
    P2Important issues not at the top of the work list
    on Mar 7, 2023
  4. github-actions commented on Mar 31, 2023

    @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.

  5. locked as resolved and limited conversation to collaborators on Mar 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listfound in release: 3.7Found to occur in 3.7found in release: 3.9Found to occur in 3.9has 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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions