Some of you may see some hints in the IDE:

Don't create a lambda when a tear-off will do
Copy the code

This means that instead of creating an anonymous function, you can use tear-off.

So what is tear-off?

Check out the DartLang documentation:

17.21 Property ExtractionFunction objects derived from members via closurization are colloquially known as tear-offs. Definition: A function object derived from a member through a closure is popularly known as: tear-offProperty extractionallows for a member to be accessed as a property rather than a function. A property Extraction can be either. A case of a closure lets class members access An instance method closurization as properties, which case a method into a function object (3.21.3). Invocation, which returns the result of invoking of A getter method (17.21.1).// Or A getter method, Returns the result triggered by the getter methodCopy the code

In other words, “tear-off” is the term used to describe the behavior of generating a function object from a function or method name.

They are equivalent to function Pointers or member function Pointers in other languages. When you want to use a function or method directly as a callback, you use tear-off.

In other words, for example, “tear-off” is a term used to describe the behavior of generating a function object from a function or method name. They are equivalent to function Pointers or member function Pointers in other languages.

When you want to use a function or method directly as a callback, you use tear-off.

Such as:

class Foo { int value; Foo(this.value); int add(int other) => value + other; int multiply(int other) => value * other; } void main() { var foo = Foo(5); // `foo.add` is an instance method tear-off. It is equivalent to: // `(int other) => foo.add(other)` var someOperation = foo.add; Print (someOperation(8)); // Prints: 13 someOperation = foo.multiply; print(someOperation(8)); // Prints: 104 }Copy the code

Don’t get it?

This example is simple enough:

One might write:

ElevatedButton( onPressed: () { myHandler(); })Copy the code

I could just write it like this

onPressed : myHandler //

ElevatedButton(
  onPressed: myHandler <-- myHandler is a tear-off
)
Copy the code

Conclusion: Many times the UI layout, such as a ElavatedButton need to click on the method, we provide this method in most cases is not call this method, so the widget can when used to call, so we need to just transfer method as a parameter, or function object, without having to write an anonymous function, Note that the function parameter and return value types must be the same