[NNBD] Function.apply fail for nullable types (online dartpad runs, but offline Dart 2.12.0 fails)

Hi thanks for this language! I find a bug, which causes a popular library cannot run 🙁

Use the following code:

void main() {
  test<int?>(42);
  test<int?>(null); // THIS LINE WILL FAIL WHEN Function.apply !!!
}

void test<T>(T value) {
  final f = (T inner) {
    print('f inner=$inner T=$T');
  };
  Function.apply(f, [value]);
}

In test<int?>(null), it should work, since T is int?. But actually it throws: For example, copy it to a.dart then run dart a.dart. Output:

f inner=42 T=int?
Unhandled exception:
type 'Null' is not a subtype of type 'T' of 'inner'
#0      _TypeError._throwNew (dart:core-patch/errors_patch.dart:98:34)
#1      Function._apply (dart:core-patch/function_patch.dart:11:30)
#2      Function.apply (dart:core-patch/function_patch.dart:34:12)
#3      test (file:///Users/tom/RefCode/bug/a.dart:10:12)
#4      main (file:///Users/tom/RefCode/bug/a.dart:3:3)
#5      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:283:19)
#6      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

When using https://nullsafety.dartpad.dev/, the code run normally. But when using dart newly installed on my machine (I even uninstall & reinstall it), it throws the errors above.

  • Dart SDK Version (dart --version) Dart SDK version: 2.12.0 (stable) (Thu Feb 25 19:50:53 2021 +0100) on "macos_x64"
  • Whether you are using Windows, MacOSX, or Linux (if applicable) macos
  • Whether you are using Chrome, Safari, Firefox, Edge (if applicable) N/A

1 possible answer(s) on “[NNBD] Function.apply fail for nullable types (online dartpad runs, but offline Dart 2.12.0 fails)