Skip to content

Type inference error occurred during compilation (Dart 2.19.2) #51431

Closed
@ipcjs

Description

@ipcjs

When running the demo_220216 with Dart 2.19.2 and setting the minimum SDK version to 2.17.0, a type inference error occurs. However, with Flutter 3.3/Dart 2.18, there is no problem. Setting the minimum SDK version to 2.18.0+ also does not cause any issues.

Dart Version: Dart SDK version: 2.19.2 (stable) (Tue Feb 7 18:37:17 2023 +0000) on "macos_arm64"

Demo

Code:
demo_220216.zip

bin/demo_220216.dart:

class Value<T> {
  const Value({
    required this.getter,
    required this.setter,
  });
  final T Function() getter;
  final void Function(T value) setter;
}

final v = Value(
  getter: () => 1,
  setter: (value) {
    int intValue = value;
    print(intValue);
  },
);

void main() {
  print(v);
}

pubspec.yaml:

environment:
  # sdk: '>=2.19.0 <3.0.0' # Success
  # sdk: '>=2.18.0 <3.0.0' # Success
  sdk: '>=2.17.0 <3.0.0' # Error: A value of type 'Object?' can't be assigned to a variable of type 'int'.

Except Output:

Instance of 'Value<int>'

Actual Output(Compile error):

bin/demo_220216.dart:13:20: Error: A value of type 'Object?' can't be assigned to a variable of type 'int'.
 - 'Object' is from 'dart:core'.
    int intValue = value;
                   ^

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions