Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the exclamation mark mean before a function call?

Tags:

I was following a PR for Flutter and came across this code:

if (chunkCallback != null) {   chunkCallback!(0, 100); } 

What does the exclamation mark mean after chunkCallback? Nothing I search on Google works.

like image 487
David Avatar asked Aug 04 '20 18:08

David


People also ask

What is an exclamation point before a number?

Factorial: Denoted by the exclamation mark (!). Factorial means to multiply by decreasing positive integers. For example, 5! = 5 ∗ 4 ∗ 3 ∗ 2 ∗ 1 = 120. Note: 0!

What does a exclamation mark symbolize?

The exclamation mark, !, or exclamation point (American English), is a punctuation mark usually used after an interjection or exclamation to indicate strong feelings or to show emphasis. The exclamation mark often marks the end of a sentence, for example: "Watch out!".


2 Answers

"!" is a new dart operator for conversion from a nullable to a non-nullable type. Read here and here about sound null safety.

Use it only if you are absolutely sure that the value will never be null and do not confuse it with the conditional property access operator.

like image 88
Spatz Avatar answered Nov 23 '22 10:11

Spatz


chunkCallback is a nullable reference to a function.

If you are sure that chunkCallback can't be null at runtime you can "Cast away nullability" by adding ! to it to make compiler happy

typedef WebOnlyImageCodecChunkCallback = void Function(     int cumulativeBytesLoaded, int expectedTotalBytes);  ...  class Class1 {   final WebOnlyImageCodecChunkCallback? chunkCallback;    Class1(this.chunkCallback);    void test() {     if (chunkCallback == null) {       throw Exception("chunkCallback is null");     }      chunkCallback!.call(0, 100);   } } 

Esentially, ! in this case is a syntactic sugar for

(chunkCallback as WebOnlyImageCodecChunkCallback).call(0, 100); 
like image 29
Pavel Shastov Avatar answered Nov 23 '22 10:11

Pavel Shastov