无夜之星辰
无夜之星辰

Reputation: 6158

What's the difference between null and ()=>null?

I am new to dart and puzzled with null_closures.

For example:

void _test1({VoidCallback callback}) {
  callback();
}

Would crash when I used as:

_test1(callback: null);

But work well as:

_test1(callback: () => null);

Why?

Upvotes: 0

Views: 43

Answers (1)

jamesdlin
jamesdlin

Reputation: 89965

_test1(callback: null);

In this case, callback is null. Attempting to dereference null by accessing properties (with some exceptions) or by calling methods on it will result in an exception. (In most other languages, this would be a null pointer exception. In Dart, null is a special object (of type Null), so attempting to call methods on it instead usually will throw a NoSuchMethod error.)

_test1(callback: () => null);

In this case, callback is a function (an object of type Function) that returns null when that function is invoked.

Upvotes: 3

Related Questions