What is the null-forgiving operator (!) and when is it appropriate?
Short answer
! tells the compiler you know a value isn't null, suppressing the warning. Use it sparingly when you have knowledge the compiler lacks — overuse hides real bugs.
Advertisement
AModel answer
! tells the compiler you know a value isn't null, suppressing the warning. Use it sparingly when you have knowledge the compiler lacks — overuse hides real bugs.
var name = dict[key]!; // you guarantee the key existsReal-world example
Asserting non-null after a framework guarantees initialization the compiler can't see.