Skip to content

remove the requirement for required on nnbd arguments #2050

Closed
@bsutton

Description

@bsutton

When defining a named argument with a non-nullable type and no default you are required to add the required keyword.

This appears to be redundant.

From the diagnostics page:

missing_default_value_for_parameter
The parameter ‘{0}’ can’t have a value of ‘null’ because of its type, but the implicit default value is ‘null’.

Description
The analyzer produces this diagnostic when an optional parameter, whether positional or named, has a potentially non-nullable type and doesn’t specify a default value. Optional parameters that have no explicit default value have an implicit default value of null. If the type of the parameter doesn’t allow the parameter to have a value of null, then the implicit default value isn’t valid.

I've identified five use patterns for named arguments:

abstract class test {
  void one({required String name});

  void two({String name = 'hi'});

  void three({String? name});

  void four({String? name = 'hi'});

  void five({required String? name});
}

The option for required with pattern five makes sense, the requirement on one appears to be redundant.

Given most of my usage of named arguments is for non-nullable types this redundant requirement is somewhat annoying.

It would appear that we could remove this requirement and provide a lint to advise that it can be removed.

I don't think this would require any migration as it simply becomes optional.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions