Skip to content

Instantly share code, notes, and snippets.

View RedBrogdon's full-sized avatar
💭
Flutterin'.

Andrew Brogdon RedBrogdon

💭
Flutterin'.
View GitHub Profile
@RedBrogdon
RedBrogdon / main.dart
Created June 5, 2020 22:02
Testing null safety
void main() {
print(const [null, 3] is List<int>);
print([null, 3] is List<int>);
}
@RedBrogdon
RedBrogdon / main.dart
Last active July 21, 2020 00:45
Snippet 11: Late and lazy
// Here's another pattern that `late` can
// help with: lazy initialization for
// expensive non-nullable fields.
//
// Try running this code without changing
// it. What do you think will change if
// you make `_cache` a `late` field?
int _computeValue() {
print('Computing value...');
@RedBrogdon
RedBrogdon / main.dart
Last active June 9, 2020 16:43
Snippet 10: Late circular references
// The `late` keyword can be really helpful
// for tricky patterns like circular
// references. Here are two objects that
// need to maintain non-nullable references
// to each other. Try using the `late`
// keyword to fix this code.
class Team {
final Coach coach;
}
@RedBrogdon
RedBrogdon / main.dart
Last active June 9, 2020 16:43
Snippet 9: The late keyword
// Sometimes fields in a class *should* be
// non-nullable, but can't be assigned a
// value right away. For cases like that,
// use the `late` keyword. It's a way to
// tell Dart that:
//
// * You aren't going to assign that
// field a value right away.
// * But you *are* going to assign it a
// value later.
@RedBrogdon
RedBrogdon / main.dart
Last active July 12, 2021 21:17
Snippet 8: The assertion operator
// If you'd like to assign a nullable expression
// to a variable that's non-nullable, you can use
// the assertion operator (the exclamation point:
// `!`). By adding `!` just after the expression,
// you tell Dart that the value won't be null,
// and it's safe to assign to a non-nullable
// variable.
//
// Note: if you're wrong, an exception will be
// thrown!
@RedBrogdon
RedBrogdon / main.dart
Last active March 22, 2022 15:22
Snippet 7: Promotion with exceptions
// Promotion works with exceptions as well
// as return statements. Try a null check
// that throws an `Exception` instead of
// returning zero.
int getLength(String? str) {
// Try throwing here if `str` is null.
return str.length;
}
@RedBrogdon
RedBrogdon / main.dart
Last active October 24, 2020 11:18
Snippet 6: Promotion
// With null safety, Dart takes null checks
// into account. Nullable variables that
// can't possibly contain null are treated
// like non-nullable variables.
//
// This behavior is called "promotion."
//
// In the example below, add an if-then to
// the beginning of `getLength` that
// returns zero if `str` is null.
@RedBrogdon
RedBrogdon / main.dart
Last active June 9, 2020 16:42
Snippet 5: Conditional access
// Conditional access is a handy way to
// tighten up code that needs to read
// properties that could be null:
//
// a?.b
//
// The expression above evaluates to the
// value of `b` as long as `a` isn’t
// null. If `a` is null, then the expression
// evaluates to null. Try using conditional
@RedBrogdon
RedBrogdon / main.dart
Last active June 9, 2020 16:42
Snippet 4: Definite assignment
// Dart's type system is crafty enough to track
// where variables are assigned and where their
// values are read, and to verify that
// non-nullable fields are given values
// before any code tries to read from them.
//
// This process is called "definite assignment."
//
// Try uncommenting the if-else statement in the
// code below, and watch the analyzer errors
@RedBrogdon
RedBrogdon / main.dart
Last active October 20, 2020 15:12
Snippet 3: More nullable types!
// Type parameters for generics can also be nullable or
// non-nullable. Try using question marks to correct the
// type declarations of `aNullableListOfStrings` and
// `aListOfNullableStrings`:
void main() {
List<String> aListofStrings = ['one', 'two', 'three'];
List<String> aNullableListOfStrings;
List<String> aListofNullableStrings = ['one', null, 'three'];