Skip to content

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

@ValentinVignal

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)

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