Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds null safety codelab #2983

Merged
merged 82 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
0a34e1a
adds examples for null safety codelab
ryanmcginnis Feb 22, 2021
05f16d5
adds null safety codelab and updates side-nav
ryanmcginnis Feb 22, 2021
e11c8fa
Update null-safety.md
ryanmcginnis Feb 22, 2021
84940e4
adds regex for first example
ryanmcginnis Feb 22, 2021
ea6f717
adds regex for more_nullable_types and nullable_types examples
ryanmcginnis Feb 22, 2021
f0c18f2
fixes some typos
ryanmcginnis Feb 22, 2021
44655b6
fixes a typo
ryanmcginnis Feb 22, 2021
04f185b
.
ryanmcginnis Feb 22, 2021
358b646
ran refresher script
ryanmcginnis Feb 22, 2021
407f8a0
adds regex for definite_assignment
ryanmcginnis Feb 22, 2021
e3ae996
fixes a typo in part of the definite_assignment regex
ryanmcginnis Feb 22, 2021
2ee2068
.
ryanmcginnis Feb 23, 2021
e521724
.
ryanmcginnis Feb 23, 2021
c9f37be
.
ryanmcginnis Feb 23, 2021
00c1028
fixes a typo!!!!
ryanmcginnis Feb 23, 2021
790110c
finished regex for definite_assignment
ryanmcginnis Feb 23, 2021
a23209b
adds remaining regexes
ryanmcginnis Feb 23, 2021
404fb00
more changes to regexes
ryanmcginnis Feb 23, 2021
47076cf
Update null_safety_examples/null_safety_codelab/bin/conditional_acces…
ryanmcginnis Feb 23, 2021
f05c4b8
Update src/codelabs/null-safety.md
ryanmcginnis Feb 23, 2021
f90e9c0
Update src/codelabs/null-safety.md
ryanmcginnis Feb 23, 2021
9e3de22
Update src/codelabs/null-safety.md
ryanmcginnis Feb 23, 2021
2051532
Update src/codelabs/null-safety.md
ryanmcginnis Feb 23, 2021
cd59884
fixes exception sample
ryanmcginnis Feb 23, 2021
07fa369
Update src/codelabs/null-safety.md
ryanmcginnis Feb 23, 2021
fa45011
80-char word wrap & pass for semanatic breaks
ryanmcginnis Feb 23, 2021
447222e
comments out failing test looking for a package that doesn't exist
ryanmcginnis Feb 23, 2021
243f072
Merge branch 'rrm-null-safety' of https://github.com/dart-lang/site-w…
ryanmcginnis Feb 23, 2021
966cd49
one more 80-char fix
ryanmcginnis Feb 23, 2021
aa09169
.
ryanmcginnis Feb 23, 2021
f146337
.
ryanmcginnis Feb 23, 2021
c524262
Update src/codelabs/null-safety.md
ryanmcginnis Feb 23, 2021
85ab744
Update src/codelabs/null-safety.md
ryanmcginnis Feb 23, 2021
a95c04d
Update src/codelabs/null-safety.md
ryanmcginnis Feb 23, 2021
38378ce
Update src/codelabs/null-safety.md
ryanmcginnis Feb 23, 2021
d16d779
Update src/codelabs/null-safety.md
ryanmcginnis Feb 23, 2021
094793a
Update src/codelabs/null-safety.md
ryanmcginnis Feb 23, 2021
473f367
Update src/codelabs/null-safety.md
ryanmcginnis Feb 23, 2021
1d8cd8d
Update src/codelabs/null-safety.md
ryanmcginnis Feb 23, 2021
250662d
addresses latest feedback
ryanmcginnis Feb 23, 2021
058bed7
.
ryanmcginnis Feb 24, 2021
83a85e7
Update src/codelabs/null-safety.md
ryanmcginnis Feb 24, 2021
05e8590
Update src/codelabs/null-safety.md
ryanmcginnis Feb 24, 2021
7345b89
Update src/codelabs/null-safety.md
ryanmcginnis Feb 24, 2021
be97864
Update src/codelabs/null-safety.md
ryanmcginnis Feb 24, 2021
59d1d81
Update src/codelabs/null-safety.md
ryanmcginnis Feb 24, 2021
12800f1
Update src/codelabs/null-safety.md
ryanmcginnis Feb 24, 2021
bffd7d6
addresses latest feedback; removes conditional access section; rewrit…
ryanmcginnis Feb 24, 2021
a971ae0
removes page-level styling
ryanmcginnis Feb 24, 2021
a2236da
moves definite assignment example to type promotion section
ryanmcginnis Feb 24, 2021
ec71b63
addresses Mit's feedback
ryanmcginnis Feb 26, 2021
24f27dd
s/Using ?, !, and late/Using ? and 1/ (because we don't talk about la…
ryanmcginnis Feb 26, 2021
2576979
Update src/codelabs/null-safety.md
ryanmcginnis Mar 12, 2021
33ff0f6
Update src/codelabs/null-safety.md
ryanmcginnis Mar 12, 2021
515f659
Update src/codelabs/null-safety.md
ryanmcginnis Mar 12, 2021
7bedbf0
Update src/codelabs/null-safety.md
ryanmcginnis Mar 12, 2021
3a5bb08
Update src/codelabs/null-safety.md
ryanmcginnis Mar 12, 2021
602bd46
Update src/codelabs/null-safety.md
ryanmcginnis Mar 12, 2021
1e2e9ed
Update src/codelabs/null-safety.md
ryanmcginnis Mar 12, 2021
098ca1b
Update src/codelabs/null-safety.md
ryanmcginnis Mar 12, 2021
97c80d7
Update src/codelabs/null-safety.md
ryanmcginnis Mar 12, 2021
6f5f3aa
Update src/codelabs/null-safety.md
ryanmcginnis Mar 12, 2021
5df1e05
Update src/codelabs/null-safety.md
ryanmcginnis Mar 17, 2021
360f73e
Update src/codelabs/null-safety.md
ryanmcginnis Mar 17, 2021
79c32de
Update src/codelabs/null-safety.md
ryanmcginnis Mar 17, 2021
0ea433c
Update src/codelabs/null-safety.md
ryanmcginnis Mar 17, 2021
f13c023
Update src/codelabs/null-safety.md
ryanmcginnis Mar 17, 2021
bb57f73
Update src/codelabs/null-safety.md
ryanmcginnis Mar 17, 2021
d365d8b
Update src/codelabs/null-safety.md
ryanmcginnis Mar 17, 2021
1744e34
Update src/codelabs/null-safety.md
ryanmcginnis Mar 17, 2021
033ee33
Update src/codelabs/null-safety.md
ryanmcginnis Mar 17, 2021
c462268
Update src/codelabs/null-safety.md
ryanmcginnis Mar 17, 2021
661299a
Update src/codelabs/null-safety.md
ryanmcginnis Mar 17, 2021
8cbc420
Update src/codelabs/null-safety.md
ryanmcginnis Mar 17, 2021
90def0c
addresses latest feedback
ryanmcginnis Mar 17, 2021
ce7e614
nit
ryanmcginnis Mar 17, 2021
3e8be92
Create a different ga_id for each example
kwalrath Mar 20, 2021
3419fb7
Minor tweaks
kwalrath Mar 20, 2021
84c019e
Merge branch 'master' into pr/2983
kwalrath Mar 20, 2021
7b9ab3d
final cleanup?
kwalrath Mar 20, 2021
2b94c36
Remove references to examples (they're all exercises)
kwalrath Mar 20, 2021
31c99f3
LateInitializationError -> error
kwalrath Mar 22, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Comment on lines +2 to +4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems very not "Dart" like to include a setter method like this. Perhaps you could switch this example to use a getter and setter:

Late variables aren't generally a good idea to expose publicly anyway as you don't have much control over what others will do.

class Meal {
  late String _description;

  void set description(String str) => _description = str;

  String get description => _description;
}

void main() {
  final myMeal = Meal();
  myMeal.description = 'Feijoada!';
  print(myMeal.description);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Goodness, this sounds reasonable but we might need to think about it. I'll add it to that follow-up issue.

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'];
Comment on lines +2 to +4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like of should be Of to be consistent with the middle declaration and general naming standards.


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
ryanmcginnis marked this conversation as resolved.
Show resolved Hide resolved
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