Last active
May 17, 2019 21:55
-
-
Save redbrogdon/f0d6e5f14e4606befa388f52eebc2647 to your computer and use it in GitHub Desktop.
DartPad Cheatsheet Codelab - 5 - Collection literals
This file contains hidden or 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 declaration after each equals sign. | |
| See the codelab text for the correct syntax to use. |
This file contains hidden or 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 hidden or 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 hidden or 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 be a 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 be a 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 be a Map<String, int>.'); | |
| } else if (aMapOfStringsToInts.length != 1) { | |
| errs.add('aMapOfStringsToInts has ${aMapOfStringsToInts.length} items in it, rather than the expected 1.'); | |
| } 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 be a List<double>.'); | |
| } else if (anEmptyListOfDouble.isNotEmpty) { | |
| errs.add('anEmptyListOfDouble should be empty.'); | |
| } | |
| if (anEmptySetOfString is! Set<String>) { | |
| errs.add('anEmptySetOfString should be a Set<String>.'); | |
| } else if (anEmptySetOfString.isNotEmpty) { | |
| errs.add('anEmptySetOfString should be empty.'); | |
| } | |
| if (anEmptyMapOfDoublesToInts is! Map<double, int>) { | |
| errs.add('anEmptyMapOfDoublesToInts should be a 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