Dart Getter Shorthand to Cache Computed Properties

Last reviewed in January 2020 by Frank Treacy

An elegant Dart getter shorthand used to cache computed properties:

T get foo => _foo ??= _computeFoo();

// which depends on having
T _foo;
T _computeFoo() => /** ... **/;

It makes use of the fallback assignment operator ??=.

Check out Null-Aware Operators in Dart for a complete guide on dealing with nulls in Dart!

The best from the Flutter-verse in 3 minutes or less? Join Snacks!

Delivered twice monthly. No link walls. No spam. EVER.