Examples:
late final for a local variable.: syntax in constructors when possible, rather than late final.What other examples might we want?
/cc @munificent @mit-mit @johnpryan @kevmoo
It's more like avoid late final if it's not needed. It may be a subtle thing to communicate.
Another example from @srawlins:
String? s = "hello";
if (s?.isNotEmpty ?? false) {
// s is not promoted to String.
print(s!.length);
}
means that variables are not promoted to non-nullable. But if you re-wrote it:
String? s = "hello";
if (s != null && s.isNotEmpty) {
// s is promoted to String.
print(s.length);
}
then you get nice promotion. This only works for variables, not for property access, so list != null && list.isNotEmpty && list.first != null does not promote list.first. But I think non-nullable promotions are a very good reason to change our thinking on ?? false.
Marking this p2 because Effective Dart often lags a bit... but I'd love to have this ready for the stable release.
Most helpful comment
Another example from @srawlins:
means that variables are not promoted to non-nullable. But if you re-wrote it:
then you get nice promotion. This only works for variables, not for property access, so
list != null && list.isNotEmpty && list.first != nulldoes not promote list.first. But I think non-nullable promotions are a very good reason to change our thinking on?? false.