Skip to content

Commit

Permalink
adds null safety codelab (#2983)
Browse files Browse the repository at this point in the history
Co-authored-by: Parker Lougheed <[email protected]>
Co-authored-by: Filip Hracek <[email protected]>
Co-authored-by: Kathy Walrath <[email protected]>
  • Loading branch information
4 people authored Mar 22, 2021
1 parent ca885fc commit 63206b9
Show file tree
Hide file tree
Showing 16 changed files with 464 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: ../analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
int? couldReturnNullButDoesnt() => -3;

void main() {
int? couldBeNullButIsnt = 1;
List<int?> listThatCouldHoldNulls = [2, null, 4];

int a = couldBeNullButIsnt;
int b = listThatCouldHoldNulls.first!; // first item in the list
int c = couldReturnNullButDoesnt()!.abs(); // absolute value

print('a is $a.');
print('b is $b.');
print('c is $c.');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class BigThing {
LittleThing little = LittleThing();
}

class LittleThing {
int fetchInt() => 12;
}

void main() {
BigThing? big = BigThing();

print('The value is:');
print(big?.little.fetchInt());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
void main() {
String text;

if (DateTime.now().hour < 12) {
text = "It's morning! Let's make aloo paratha!";
} else {
text = "It's afternoon! Let's make biryani!";
}

print(text);
print(text.length);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Team {
late final Coach coach;
}

class Coach {
late final Team team;
}

void main() {
final myTeam = Team();
final myCoach = Coach();
myTeam.coach = myCoach;
myCoach.team = myTeam;

print('All done!');
}
13 changes: 13 additions & 0 deletions null_safety_examples/null_safety_codelab/bin/late_keyword.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Meal {
late String description;

void setDescription(String str) {
description = str;
}
}

void main() {
final myMeal = Meal();
myMeal.setDescription('Feijoada!');
print(myMeal.description);
}
16 changes: 16 additions & 0 deletions null_safety_examples/null_safety_codelab/bin/late_lazy.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
int _computeValue() {
print('In _computeValue...');
return 3;
}

class CachedValueProvider {
final _cache = _computeValue();
int get value => _cache;
}

void main() {
print('Calling constructor...');
var provider = CachedValueProvider();
print('Getting value...');
print('The value is ${provider.value}!');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
void main() {
List<String> aListofStrings = ['one', 'two', 'three'];
List<String?> aNullableListOfStrings = [];
List<String?> aListofNullableStrings = ['one', null, 'three'];

print('aListofStrings is $aListofStrings.');
print('aNullableListOfStrings is $aNullableListOfStrings.');
print('aListofNullableStrings is $aListofNullableStrings.');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void main() {
int a;
a = 145;
print('a is $a.');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void main() {
int? a;
a = null;
print('a is $a.');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int getLength(String? str) {
// Try throwing an exception here if `str` is null.
if (str == null) {
throw Exception("String is null");
}
return str.length;
}

void main() {
print(getLength(null));
}
11 changes: 11 additions & 0 deletions null_safety_examples/null_safety_codelab/bin/type_promotion.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int getLength(String? str) {
// Add null check here
if (str == null) {
return 0;
}
return str.length;
}

void main() {
print(getLength('This is a string!'));
}
10 changes: 10 additions & 0 deletions null_safety_examples/null_safety_codelab/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: null_safety_codelab
description: dart.dev example code for null safety codelab

environment:
sdk: ">=2.12.0-0 <3.0.0"

dev_dependencies:
examples_util: {path: ../util}
pedantic: ^1.10.0-nullsafety.3
test: ^1.16.0-nullsafety.13
2 changes: 2 additions & 0 deletions src/_data/side-nav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
permalink: /codelabs/iterables
- title: Asynchronous programming
permalink: /codelabs/async-await
- title: Null safety
permalink: /codelabs/null-safety
- title: Tutorials
urlExactMatch: true
match-page-url-exactly: true
Expand Down
Loading

0 comments on commit 63206b9

Please sign in to comment.