Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
Proposal: allow passing constructors as functions #35901
This feature request intends to allow using constructors where functions with the same type/signature are expected.
As of Dart 2, Effective Dart suggests not using the
The Language Tour says (emphasis mine):
Thus suggesting that a constructor is a function. But it turns out it is not really a function, as it cannot be used in all contexts where a function can.
Why it would be useful
Dart built in types, in particular collection types, have several methods that take functions as arguments, for instance
In an program I'm working on, I got a bit surprised by not being able to create new instances using
Example Flutter code
What I would like to write:
return Column( // This won't compile: children: ['Alice', 'Bob', 'Charlie'].map(Text).toList(), );
What I have to write instead with Dart 2.1.0:
return Column( // Need a dummy wrapper around the constructor: children: ['Alice', 'Bob', 'Charlie'].map((name) => Text(name)).toList(), );
Note that the feature request is not specific to Flutter code, but applies to any Dart code as per the more generic example in https://dartpad.dartlang.org/9745b0f73157959a1c82a66ddf8fdba4.