Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing optional (nullable) `Function` in Dart [duplicate]

Tags:

flutter

dart

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?

like image 707
devAdam Avatar asked Aug 29 '26 13:08

devAdam


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.

like image 174
Nisanth Reddy Avatar answered Aug 31 '26 02:08

Nisanth Reddy