-
Notifications
You must be signed in to change notification settings - Fork 704
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
adds null safety codelab #2983
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 05f16d5
adds null safety codelab and updates side-nav
ryanmcginnis e11c8fa
Update null-safety.md
ryanmcginnis 84940e4
adds regex for first example
ryanmcginnis ea6f717
adds regex for more_nullable_types and nullable_types examples
ryanmcginnis f0c18f2
fixes some typos
ryanmcginnis 44655b6
fixes a typo
ryanmcginnis 04f185b
.
ryanmcginnis 358b646
ran refresher script
ryanmcginnis 407f8a0
adds regex for definite_assignment
ryanmcginnis e3ae996
fixes a typo in part of the definite_assignment regex
ryanmcginnis 2ee2068
.
ryanmcginnis e521724
.
ryanmcginnis c9f37be
.
ryanmcginnis 00c1028
fixes a typo!!!!
ryanmcginnis 790110c
finished regex for definite_assignment
ryanmcginnis a23209b
adds remaining regexes
ryanmcginnis 404fb00
more changes to regexes
ryanmcginnis 47076cf
Update null_safety_examples/null_safety_codelab/bin/conditional_acces…
ryanmcginnis f05c4b8
Update src/codelabs/null-safety.md
ryanmcginnis f90e9c0
Update src/codelabs/null-safety.md
ryanmcginnis 9e3de22
Update src/codelabs/null-safety.md
ryanmcginnis 2051532
Update src/codelabs/null-safety.md
ryanmcginnis cd59884
fixes exception sample
ryanmcginnis 07fa369
Update src/codelabs/null-safety.md
ryanmcginnis fa45011
80-char word wrap & pass for semanatic breaks
ryanmcginnis 447222e
comments out failing test looking for a package that doesn't exist
ryanmcginnis 243f072
Merge branch 'rrm-null-safety' of https://github.com/dart-lang/site-w…
ryanmcginnis 966cd49
one more 80-char fix
ryanmcginnis aa09169
.
ryanmcginnis f146337
.
ryanmcginnis c524262
Update src/codelabs/null-safety.md
ryanmcginnis 85ab744
Update src/codelabs/null-safety.md
ryanmcginnis a95c04d
Update src/codelabs/null-safety.md
ryanmcginnis 38378ce
Update src/codelabs/null-safety.md
ryanmcginnis d16d779
Update src/codelabs/null-safety.md
ryanmcginnis 094793a
Update src/codelabs/null-safety.md
ryanmcginnis 473f367
Update src/codelabs/null-safety.md
ryanmcginnis 1d8cd8d
Update src/codelabs/null-safety.md
ryanmcginnis 250662d
addresses latest feedback
ryanmcginnis 058bed7
.
ryanmcginnis 83a85e7
Update src/codelabs/null-safety.md
ryanmcginnis 05e8590
Update src/codelabs/null-safety.md
ryanmcginnis 7345b89
Update src/codelabs/null-safety.md
ryanmcginnis be97864
Update src/codelabs/null-safety.md
ryanmcginnis 59d1d81
Update src/codelabs/null-safety.md
ryanmcginnis 12800f1
Update src/codelabs/null-safety.md
ryanmcginnis bffd7d6
addresses latest feedback; removes conditional access section; rewrit…
ryanmcginnis a971ae0
removes page-level styling
ryanmcginnis a2236da
moves definite assignment example to type promotion section
ryanmcginnis ec71b63
addresses Mit's feedback
ryanmcginnis 24f27dd
s/Using ?, !, and late/Using ? and 1/ (because we don't talk about la…
ryanmcginnis 2576979
Update src/codelabs/null-safety.md
ryanmcginnis 33ff0f6
Update src/codelabs/null-safety.md
ryanmcginnis 515f659
Update src/codelabs/null-safety.md
ryanmcginnis 7bedbf0
Update src/codelabs/null-safety.md
ryanmcginnis 3a5bb08
Update src/codelabs/null-safety.md
ryanmcginnis 602bd46
Update src/codelabs/null-safety.md
ryanmcginnis 1e2e9ed
Update src/codelabs/null-safety.md
ryanmcginnis 098ca1b
Update src/codelabs/null-safety.md
ryanmcginnis 97c80d7
Update src/codelabs/null-safety.md
ryanmcginnis 6f5f3aa
Update src/codelabs/null-safety.md
ryanmcginnis 5df1e05
Update src/codelabs/null-safety.md
ryanmcginnis 360f73e
Update src/codelabs/null-safety.md
ryanmcginnis 79c32de
Update src/codelabs/null-safety.md
ryanmcginnis 0ea433c
Update src/codelabs/null-safety.md
ryanmcginnis f13c023
Update src/codelabs/null-safety.md
ryanmcginnis bb57f73
Update src/codelabs/null-safety.md
ryanmcginnis d365d8b
Update src/codelabs/null-safety.md
ryanmcginnis 1744e34
Update src/codelabs/null-safety.md
ryanmcginnis 033ee33
Update src/codelabs/null-safety.md
ryanmcginnis c462268
Update src/codelabs/null-safety.md
ryanmcginnis 661299a
Update src/codelabs/null-safety.md
ryanmcginnis 8cbc420
Update src/codelabs/null-safety.md
ryanmcginnis 90def0c
addresses latest feedback
ryanmcginnis ce7e614
nit
ryanmcginnis 3e8be92
Create a different ga_id for each example
kwalrath 3419fb7
Minor tweaks
kwalrath 84c019e
Merge branch 'master' into pr/2983
kwalrath 7b9ab3d
final cleanup?
kwalrath 2b94c36
Remove references to examples (they're all exercises)
kwalrath 31c99f3
LateInitializationError -> error
kwalrath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include: ../analysis_options.yaml |
14 changes: 14 additions & 0 deletions
14
null_safety_examples/null_safety_codelab/bin/assertion_operator.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} |
14 changes: 14 additions & 0 deletions
14
null_safety_examples/null_safety_codelab/bin/conditional_access.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
12 changes: 12 additions & 0 deletions
12
null_safety_examples/null_safety_codelab/bin/definite_assignment.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
16 changes: 16 additions & 0 deletions
16
null_safety_examples/null_safety_codelab/bin/late_circular_references.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
null_safety_examples/null_safety_codelab/bin/late_keyword.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
null_safety_examples/null_safety_codelab/bin/late_lazy.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}!'); | ||
} |
9 changes: 9 additions & 0 deletions
9
null_safety_examples/null_safety_codelab/bin/more_nullable_types.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like |
||
|
||
print('aListofStrings is $aListofStrings.'); | ||
print('aNullableListOfStrings is $aNullableListOfStrings.'); | ||
print('aListofNullableStrings is $aListofNullableStrings.'); | ||
} |
5 changes: 5 additions & 0 deletions
5
null_safety_examples/null_safety_codelab/bin/non_nullable_types.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
void main() { | ||
int a; | ||
a = 145; | ||
print('a is $a.'); | ||
} |
5 changes: 5 additions & 0 deletions
5
null_safety_examples/null_safety_codelab/bin/nullable_types.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
void main() { | ||
int? a; | ||
a = null; | ||
print('a is $a.'); | ||
} |
11 changes: 11 additions & 0 deletions
11
null_safety_examples/null_safety_codelab/bin/promotion_exceptions.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
null_safety_examples/null_safety_codelab/bin/type_promotion.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.