Site-www: Update Effective Dart for null safety

Created on 26 May 2020  路  3Comments  路  Source: dart-lang/site-www

Examples:

  • Don't use late final for a local variable.
  • Use : syntax in constructors when possible, rather than late final.

What other examples might we want?

/cc @munificent @mit-mit @johnpryan @kevmoo

EffectiveDart blocked e1-hours needs-info null-safety p2-medium

Most helpful comment

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.

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings