[Analyzer] Type of function literal can’t be inferred because the literal has a block as its body

Dart version and OS

  • Dart SDK Version: 2.4.0 (Wed Jun 19 11:53:45 2019 +0200) on “windows_x64”
  • Also reproducible with DartPad.

I saw the following Analyzer problem on the Gitter channel and I thought it was something which should be reported:

// Type of t1 are Analyzed to be: Iterable<Null>
var t1 = [1].map((value) {
  // info: The type of the function literal can't be inferred because the literal has a block as its body
  return value.toString();
});

// works
// Type of t2 are Analyzed to be: Iterable<String>
var t2 = [1].map((value) => value.toString());

main() {
  // Type of t3 are Analyzed to be: Iterable<String>
  var t3 = [1].map((value) {
    // works
    return value.toString();
  });

  // works
  // Type of t4 are Analyzed to be: Iterable<String>
  var t4 = [1].map((value) => value.toString()); // works

  print(t1.toList()); // works
  print(t2.toList()); // works
  print(t3.toList()); // works
  print(t4.toList()); // works
}

I don’t think it makes sense that the type system works different in this situation just because we are on a global context (t1) instead of a context of a method (t3).

Also, the code are running just fine both on VM and Dartpad so it seems to be the Analyzer which have some kind of a problem here.

Author: Fantashit

1 thought on “[Analyzer] Type of function literal can’t be inferred because the literal has a block as its body

Comments are closed.