Skip to content

Callable class generic type not inferred from context #62177

@arthurbcd

Description

@arthurbcd

Dart does not infer the generic type of a callable class from the context, even when the type is unambiguous. For example:

class Validator<T> {
  String? call(T? value) => null;
}

TextFormField(validator: Validator()); // T inferred as dynamic (dart 3.10.1)
  • TextFormField.validator expects String? Function(String?).
  • Declaring Validator() works, but is redundant, because any other type produces a compile-time error.
  • In theory, the compiler could infer T = String automatically, since it’s the only valid choice.
  • Dart already does this inference for closures, but not for callable classes.

This would make callable classes more ergonomic in situations like this.

Is this behavior intended? Why?
Could Dart support inferring the generic type of a callable class from the context?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions