-
-
Save kwalrath/8ba5e98559ff2a2e92e58ac5a28f1cff to your computer and use it in GitHub Desktop.
DartPad Cheatsheet Codelab - 5 - Collection literals
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
This exercise is fairly straightforward. Just add a list, set, or map literal after each equals sign. | |
See the codelab text for the correct syntax to use. |
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
// Assign this a list containing 'a', 'b', and 'c' in that order: | |
final aListOfStrings = | |
// Assign this a set containing 3, 4, and 5: | |
final aSetOfInts = | |
// Assign this a map of String to int so that aMapOfStringsToInts['myKey'] returns 12: | |
final aMapOfStringsToInts = | |
// Assign this an empty List<double>: | |
final anEmptyListOfDouble = | |
// Assign this an empty Set<String>: | |
final anEmptySetOfString = | |
// Assign this an empty Map of double to int: | |
final anEmptyMapOfDoublesToInts = |
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
// Assign this a list containing 'a', 'b', and 'c' in that order: | |
final aListOfStrings = ['a', 'b', 'c']; | |
// Assign this a set containing 3, 4, and 5: | |
final aSetOfInts = {3, 4, 5}; | |
// Assign this a map of String to int so that aMapOfStringsToInts['myKey'] returns 12: | |
final aMapOfStringsToInts = {'myKey': 12}; | |
// Assign this an empty List<double>: | |
final anEmptyListOfDouble = <double>[]; | |
// Assign this an empty Set<String>: | |
final anEmptySetOfString = <String>{}; | |
// Assign this an empty Map of double to int: | |
final anEmptyMapOfDoublesToInts = <double, int>{}; |
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
void main() { | |
final errs = <String>[]; | |
if (aListOfStrings is! List<String>) { | |
errs.add('aListOfStrings should have the type List<String>.'); | |
} else if (aListOfStrings.length != 3) { | |
errs.add('aListOfStrings has ${aListOfStrings.length} items in it, rather than the expected 3.'); | |
} else if (aListOfStrings[0] != 'a' || aListOfStrings[1] != 'b' || aListOfStrings[2] != 'c') { | |
errs.add('aListOfStrings doesn\'t contain the correct values (\'a\', \'b\', \'c\').'); | |
} | |
if (aSetOfInts is! Set<int>) { | |
errs.add('aSetOfInts should have the type Set<int>.'); | |
} else if (aSetOfInts.length != 3) { | |
errs.add('aSetOfInts has ${aSetOfInts.length} items in it, rather than the expected 3.'); | |
} else if (!aSetOfInts.contains(3) || !aSetOfInts.contains(4) || !aSetOfInts.contains(5)) { | |
errs.add('aSetOfInts doesn\'t contain the correct values (3, 4, 5).'); | |
} | |
if (aMapOfStringsToInts is! Map<String, int>) { | |
errs.add('aMapOfStringsToInts should have the type Map<String, int>.'); | |
} else if (aMapOfStringsToInts['myKey'] != 12) { | |
errs.add('aMapOfStringsToInts doesn\'t contain the correct values (\'myKey\': 12).'); | |
} | |
if (anEmptyListOfDouble is! List<double>) { | |
errs.add('anEmptyListOfDouble should have the type List<double>.'); | |
} else if (anEmptyListOfDouble.isNotEmpty) { | |
errs.add('anEmptyListOfDouble should be empty.'); | |
} | |
if (anEmptySetOfString is! Set<String>) { | |
errs.add('anEmptySetOfString should have the type Set<String>.'); | |
} else if (anEmptySetOfString.isNotEmpty) { | |
errs.add('anEmptySetOfString should be empty.'); | |
} | |
if (anEmptyMapOfDoublesToInts is! Map<double, int>) { | |
errs.add('anEmptyMapOfDoublesToInts should have the type Map<double, int>.'); | |
} else if (anEmptyMapOfDoublesToInts.isNotEmpty) { | |
errs.add('anEmptyMapOfDoublesToInts should be empty.'); | |
} | |
if (errs.isEmpty) { | |
_result(true); | |
} else { | |
_result(false, errs); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment