Null safety: Frequently asked questions
This page collects some common questions we've heard about null safety based on the experience of migrating Google internal code.
What runtime changes should I be aware of for users of migrated code?
#Most of the effects of migration do not immediately affect users of migrated code:
- Static null safety checks for users first apply when they migrate their code.
- Full null safety checks happen when all the code is migrated and sound mode is turned on.
Two exceptions to be aware of are:
-
The
!
operator is a runtime null check in all modes, for all users. So, when migrating, ensure that you only add!
where it's an error for anull
to flow to that location, even if the calling code has not migrated yet. -
Runtime checks associated with the
late
keyword apply in all modes, for all users. Only mark a fieldlate
if you are sure it is always initialized before it is used.
What if a value is only null
in tests?
#
If a value is only ever
null
in tests, the code can be improved by marking it
non-nullable and making the tests pass non-null values.
How does @required
compare to the new required
keyword?
#
The
@required
annotation marks named arguments that must be passed; if not,
the analyzer reports a hint.
With null safety, a named argument w