-
Notifications
You must be signed in to change notification settings - Fork 704
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Parker Lougheed <[email protected]> Co-authored-by: Filip Hracek <[email protected]> Co-authored-by: Kathy Walrath <[email protected]>
- Loading branch information
1 parent
ca885fc
commit 63206b9
Showing
16 changed files
with
464 additions
and
1 deletion.
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']; | ||
|
||
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 |
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.