fluttersmith/tipsGitHub ↗
Learning path · 8 tips, in order

Modern Dart

Records, patterns, sealed classes and the other Dart 3 features that change how everyday code reads. Best read in order.

  1. 010Return multiple values with a recordA record returns two or more values without a throwaway class, and destructuring unpacks them in one line at the call site.records
  2. 013Destructure lists with list patternsList patterns pull values out of a list by position, and a rest element skips or collects whatever sits in the middle.patterns
  3. 008Switch on a record to match several values at oncePut two or three values in a record and switch on it. Each case is a row in a table, instead of a tangle of if/else.patterns
  4. 028Parse JSON safely with map patternsA switch on a map pattern checks keys and value types in one go, so bad JSON reaches your fallback case instead of a TypeError.json
  5. 005A Result type with a sealed classReturn success or failure instead of throwing, and let an exhaustive switch make sure every caller handles both.sealed-classes
  6. 019Extension types vs extension methodsAn extension method adds to a type. An extension type wraps it in a new static type, so a UserId can't be passed where an OrderId belongs.extension-types
  7. 021Skip nulls in collection literals with ...? and ?valueThe null-aware spread adds nothing for a null list, and a null-aware element leaves out a null entry, so you can drop the if (x != null).collections
  8. 016Await several futures with a record's .waitFuture.wait gives you a List of Object. A record of futures with .wait keeps each type and tells you which one failed.async