[英]How do I use Dart extension functions?
Dart 2.6 introduces a new language feature called " static extension members ". Dart 2.6 引入了一个名为“ static 扩展成员”的新语言功能。
However, I do not quite understand how to use it.但是,我不太明白如何使用它。
I would like to easily get the childCount
of a Row
or Column
, ie use row.childCount
instead of row.children.length
:我想轻松获得Row
或Column
的childCount
,即使用row.childCount
而不是row.children.length
:
void main() {
final row = Row(children: const [Text('one'), Text('two')]),
column = Column(children: const [Text('one'), Text('two'), Text('three')]);
print(row.childCount); // Should print "2".
print(column.childCount); // Should print "3".
}
I tried to do the following, but it is a syntax error:我尝试执行以下操作,但这是一个语法错误:
Row.childCount() => this.children.length;
Column.childCount() => this.children.length;
There is an official video by the Flutter team about extension methods now. Flutter 团队现在有一个关于扩展方法的官方视频。
Here is an intuitive example of how extension methods work:这是扩展方法如何工作的直观示例:
extension FancyNum on num {
num plus(num other) => this + other;
num times(num other) => this * other;
}
I simply extend num
here and add methods to the class.我只是在这里扩展num
并向 class 添加方法。 This could be used like this:这可以像这样使用:
print(5.plus(3)); // Equal to "5 + 3".
print(5.times(8)); // Equal to "5 * 8".
print(2.plus(1).times(3)); // Equal to "(2 + 1) * 3".
Note that the name FancyNum
is optional and the following is valid too:请注意,名称FancyNum
是可选的,以下也是有效的:
extension on num {}
When you use your extension in another file, you must give it a name.当你在另一个文件中使用你的扩展名时,你必须给它一个名字。
The extension above will make use of implicit extension member invocations as you do not have to explicitly declare your num
to be a FancyNum
.上面的扩展将使用隐式扩展成员调用,因为您不必将num
显式声明为FancyNum
。
You can also explicitly declare your extension, but this is not needed in most cases:您也可以显式声明您的扩展,但在大多数情况下不需要这样做:
print(FancyNum(1).plus(2));
The desired behavior from the question can be achieved by extending Row
or Column
, or even better: you can extend Flex
, which is the super class of Row
and Column
:问题的期望行为可以通过扩展Row
或Column
来实现,甚至更好:您可以扩展Flex
,这是Row
和Column
的超级 class :
extension ExtendedFlex on Flex {
int get childCount => this.children.length;
}
this.
can also be omitted if children
is not defined in the current lexical scope of childCount
, which means that => children.length
is also valid.如果 childCount 的当前词法childCount
中没有定义children
,也可以省略,这意味着=> children.length
也是有效的。
With this static extension of Flex
imported, you can call it on any Flex
, ie also on every Row
and Column
.使用这个static 扩展的Flex
导入,您可以在任何Flex
上调用它,即也可以在每个Row
和Column
上调用。
Row(children: const [Text('one'), Text('two')]).childCount
will evaluate to 2
. Row(children: const [Text('one'), Text('two')]).childCount
将评估为2
。
Dart 2.7 introduced new Extension Method concept. Dart 2.7 引入了新的扩展方法概念。
https://dart.dev/guides/language/extension-methods https://dart.dev/guides/language/extension-methods
extension ParseNumbers on String {
int parseInt() {
return int.parse(this);
}
double parseDouble() {
return double.parse(this);
}
}
main() {
int i = '42'.parseInt();
print(i);
}
Extensions can have generic type parameters.扩展可以有泛型类型参数。 For The following examples display the implicit extension resolution when multiple applicable extensions are available.对于以下示例显示多个适用扩展可用时的隐式扩展分辨率。
extension SmartIterable<T> on Iterable<T> {
void doTheSmartThing(void Function(T) smart) {
for (var e in this) smart(e);
}
}
extension SmartList<T> on List<T> {
void doTheSmartThing(void Function(T) smart) {
for (int i = 0; i < length; i++) smart(this[i]);
}
}
...
List<int> x = ....;
x.doTheSmartThing(print);
Here both the extensions apply, but the SmartList
extension is more specific than the SmartIterable
extension because List <: Iterable<dynamic>
.这里两个扩展都适用,但SmartList
扩展比SmartIterable
扩展更具体,因为 List <: Iterable<dynamic>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.