Executing optional (nullable) `Function` in Dart

Viewed 394

In some languages like Swift if you have Optional variable like below

var onItemPressed: ((Item) -> Void)?

// we can call this variable (function) like this
onItemPressed?(Item())

What's the equivalent of this in Dart language?

1 Answers

The equivalent in Dart would be the call method on functions.

Like this,

onItemPressed?.call(Item())

So basically, whenever your onItemPressed is null, the execution short-circuits itself and it won't be called resulting in avoiding the crash.

Related