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.
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!
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!".
"!"
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With