Skip to content

type '(int) => void' is not a subtype of type '(Object) => void' #57052

@tolotrasamuel

Description

@tolotrasamuel

I checked all my generics are all OK. Inheritance seems ok. Type inference seems ok. No compiler error. I don't have any hard type casting. Everything is seems strongly typed.

class ModelField {}

class DropdownField<V> extends ModelField {
  final void Function(V value) setValue;

  final List<V> options;

  DropdownField({
    required this.setValue,
    required this.options,
  });
}

void _buildDropdownField<V>(DropdownField<V> field) {
  final options = field.options;
  final data = options.first;

  field.setValue(data); // error Here
}

void _renderField(ModelField field) {
  /// from server
  // if field is StringField, else if field is DateField, ... else
  if (field is DropdownField<Object>) {
    // same thing occurs with  DropdownField<dynamic>
    _buildDropdownField(field);
  }
}

void main() {
  final field =
      DropdownField<int>(setValue: (value) => print(value), options: [1, 2, 3]);

  _renderField(field);
}

But when I run:

Unhandled exception:
type '(int) => void' is not a subtype of type '(Object) => void'
#0      _buildDropdownField (package:shared_core/weird_dart/generic_promotion.dart:18:9)
dart-lang/sdk#57147      _renderField (package:shared_core/weird_dart/generic_promotion.dart:26:5)
dart-lang/sdk#57148      main (package:shared_core/weird_dart/generic_promotion.dart:34:3)
dart-lang/sdk#57149      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
dart-lang/sdk#57150      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

How can int not be a subtype of Object???

I am so confused (or Dart is) I hope it's me. But this error occurs only at run time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    closed-as-intendedClosed as the reported issue is expected behaviorfeature-varianceImplementation of the variance feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions