Created with <3 with dartpad.dev.
Created
March 25, 2023 02:12
-
-
Save folaoluwafemi/d028f7736d1d0b13d4148d6fbe9ecd8c to your computer and use it in GitHub Desktop.
snowy-glacier-9997
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() { | |
listPatternExample(); | |
mapPatternExample(); | |
recordPatternExample(); | |
print(switchExpressionExample()); | |
print(complexSwitchExpressionExample()); | |
variablePatternExample(); | |
nullPatternExample(); | |
castPatternExample(); | |
patternInConditions(); | |
} | |
void patternInConditions() { | |
String string = 'b'; | |
if (string case 'a' || 'b' when string.isNotEmpty) { | |
print('string is a or b when not empty'); | |
} else if (string case '' when string.isEmpty) { | |
print('string is empty'); | |
} | |
} | |
void castPatternExample() { | |
final List<num> list = [1, 2.1, 3, 5.7]; | |
//final <num>[int integer, double decimal, ...] = list; //compiler error | |
final <num>[int integer as int, double decimal as double, ...] = list; //valid | |
print('$integer | $decimal'); | |
} | |
void nullPatternExample() { | |
String? myString = 'myString'; | |
List<String?> list = [null, 'hello', null, 'hi']; | |
//null assertion | |
[_, myString!, ...] = list; | |
print(myString); | |
switch (list) { | |
case [var someValue?, ...]: | |
print('null-check: $someValue'); //no output | |
case [_, var someValue!, ...]: | |
print('null-assertion: $someValue'); //throws TypeError | |
} | |
// note that null check is a refutable pattern, and hence cannot be | |
// used in assignments | |
} | |
void variablePatternExample() { | |
final (String name, int age) unnamedRecord = ('Esther', 40); | |
//destructuring1 | |
final (String username, int userAge) = unnamedRecord; | |
print('name: $username | age: $userAge'); | |
//collection destructuring | |
String value = 'value'; | |
final List<String> myList = ['element1', 'element2', 'element3']; | |
final Map<int, String> myMap = {1: 'one', 2: 'two', 3: 'three'}; | |
[..., value] = myList; | |
print('value: $value'); | |
//destructuring two variables of the same value from a list | |
String someOtherValue = 'value'; | |
String anotherValue = 'value'; | |
//List | |
<String>[someOtherValue && anotherValue, ...] = myList; | |
print('$someOtherValue | $anotherValue'); | |
//map | |
<int, String>{1: someOtherValue, 2: anotherValue, ...} = myMap; | |
print('$someOtherValue | $anotherValue'); | |
//named record destructuring sugar | |
String name = ''; | |
int age = 0; | |
final ({String name, int age}) namedRecord = (name: 'Matthew', age: 25); | |
(:name, :age) = namedRecord; | |
print('name: $name | age: $age'); | |
} | |
String switchExpressionExample() { | |
final List<int> myList = [1, 2, 3, 4, 5]; | |
switch(myList){ | |
case [] | |
} | |
final List<String> stringList = ['Flutter', 'Forward', 'Extended', "Akure"]; | |
//using the rest element (unnmatched) and variable pattern | |
final String city = switch (stringList) { | |
[..., String _city] => _city, | |
_ => 'City not found', | |
}; | |
return city; | |
} | |
String complexSwitchExpressionExample() { | |
List<({String username, int age})> usersRecord = [ | |
(username: 'Obi Okon', age: 28), | |
(username: 'Anita Latifah', age: 21), | |
(username: 'Bola Buju', age: 40), | |
]; | |
//using rest element (unnmatched), variable, constant and relational pattern, | |
return switch (usersRecord) { | |
[(username: String name && 'Badoo', age: _), ...] => | |
'Baddest guy ever liveth: $name', | |
[..., (:String username, age: 40)] => 'a young person named: $username', | |
_ => 'user not found', | |
}; | |
} | |
(String, bool) recordPatternExample() { | |
final ( | |
int unnamedInt, | |
double unnamedDouble, { | |
String namedString, | |
bool namedBool | |
}) myRecord = (2, 3.14, namedString: 'name', namedBool: false); | |
//switch use-case | |
switch (myRecord) { | |
case (_, _, :String namedString, :bool namedBool): | |
print('namedString is "$namedString" and bool value is "$namedBool"'); | |
} | |
//switch expression | |
return switch (myRecord) { | |
(_, _, :String namedString, :bool namedBool) => (namedString, namedBool), | |
}; | |
} | |
void mapPatternExample() { | |
final Map<String, int> myMap = {'hello': 1, 'hi': 2, 'thanks': 3}; | |
//switch use-case, variable pattern (destructuring) | |
switch (myMap) { | |
case {'hello': final int value, ...}: | |
print('hello value is: $value'); | |
case {'hi': == 2}: | |
print('key is hi and value is 2'); | |
} | |
} | |
void listPatternExample() { | |
final List<int> myValue = [1, 2, 3, 5, 6, 7, 9]; | |
switch (myValue) { | |
case [int a, ..., int c, int d] when myValue[4] == 6: | |
print('first value is $a some value is $c and last value is $d'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment