With the introduction of NNBD in Dart 2.12, a new keyword was created: late
. The primary reason this was created was to allow for non-null fields, that did not have to be immediately initialized.
// Allow this to be null for now, compiler trusts us, that we will assign it.
late final int i;
MyConstructor(){
i = getStartIndex(); // Compiler checks at this point, everything is ok!
i = null; // This will cause an error, compiler is enforcing non-null
}
This was a necessary feature/workaround for Flutter, because of darts requirement to use const
initializers, and most Flutter developers are likely familiar with it by now. In this post we’re going to look at some of the other benefits of late
!