我对飞镖还是新手,我想知道我的实际区别是什么,以及什么时候使用哪一个。
发布于 2018-11-30 19:12:09
函数是在类之外声明的顶级函数,或者是在另一个函数或内部方法中创建的内联函数。
方法与类的实例相关联,并具有对this的隐式引用。
main.dart
// function
void foo() => print('foo');
// function
String bar() {
return 'bar';
}
void fooBar() {
int add(int a, int b) => a + b; // inline function
int value = 0;
for(var i = 0; i < 9; i++) {
value = add(value, i); // call of inline function
print(value);
}
}
class SomeClass {
static void foo() => print('foo'); // function in class context sometimes called static method but actually not a method
SomeClass(this.firstName);
String firstName;
// a real method with implicit access to `this`
void bar() {
print('${this.firstName} bar');
print('$firstName bar'); // this can and should be omitted in Dart
void doSomething() => print('doSomething'); // inline function declared in a method
doSomething(); // call of inline function
}
}与内联函数一样,您也可以创建未命名的内联函数,也称为闭包。它们经常被用作回调,例如
button.onClick.listen( /* function start */ (event) {
print(event.name);
handleClick();
} /* function end */);https://stackoverflow.com/questions/53562578
复制相似问题