NM Naufaldo
NM Naufaldo

Reputation: 1160

How to create a default empty function in Dart

I want to pass a callback as a constructor parameter and if the callback is null I want to give a default value. But I don't know a proper way how to generate an empty function in Dart.

enter image description here

Upvotes: 3

Views: 2890

Answers (2)

Giovanni Londero
Giovanni Londero

Reputation: 1409

You need to wrap the callback in parentheses:

this.callback = callback ?? (() {})

otherwise the braces would be considered as the body of the constructor.

Upvotes: 3

Taiwo Abiodun G
Taiwo Abiodun G

Reputation: 1

Just remove the arrow function so you have this.callback = callback ?? () {}

Upvotes: 0

Related Questions