๋ฐ์ํ
https://dart.dev/codelabs/dart-cheatsheet
Null-aware operators
dart์์๋ null ์ผ ์๋ ์๋ ๊ฐ๋ค์ ๋ค๋ฃจ๋ handy operators๊ฐ ์๋ค.
ํ๋๋ ??= assignment operator : ํ์ฌ null์ผ ๊ฒฝ์ฐ๋ง ๊ฐ์ด ํ ๋น ๋๋ค.
int a; // The initial value of a is null.
a ??= 3;
print(a); // <-- Prints 3.
a ??= 5;
print(a); // <-- Still prints 3. a๋ null์ด ์๋๋ฏ๋ก! 5๋ ๋์
๋์ง ์์
๋ค๋ฅธ ํ๋๋ ?? left๊ฐ null์ด ์๋๋ฉด ์ผ์ชฝ, null์ด๋ฉด ์ค๋ฅธ์ชฝ ๊ฐ์ ๋ฐํ
print(1 ?? 3); // <-- Prints 1.
print(null ?? 12); // <-- Prints 12.
Conditional property access
null ์ผ ์๋ ์๋ object์ property๋ method์ ์ ๊ทผ์ ๋ณดํธํ๋ ค๋ฉด dot(.) ์์ (?)๋ฅผ ๋ฃ์ผ๋ฉด ๋จ
myObject?.someProperty
// ๋์ผํจ
// (myObject != null) ? myObject.someProperty : null
myObject?.someProperty?.someMethod()
Collection literals
final aListOfStrings = ['one', 'two', 'three'];
final aSetOfStrings = {'one', 'two', 'three'};
final aMapOfStringsToInts = {
'one': 1,
'two': 2,
'three': 3,
};
final aListOfInts = <int>[];
final aSetOfInts = <int>{};
final aMapOfIntToDouble = <int, double>{};
final aListOfBaseType = <BaseType>[SubType(), SubType()];
Code example
// 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>{};
Arrow syntax
=> ์ค๋ฅธ์ชฝ์ ๊ฐ์ return ํ๋ function
bool hasEmpty = aListOfStrings.any((s) {
return s.isEmpty;
});
Code example
class MyClass {
int _value1 = 2;
int _value2 = 3;
int _value3 = 5;
// Returns the product of the above values:
int get product => _value1*_value2*_value3;
// Adds 1 to _value1:
void incrementValue1() => _value1++;
// Returns a string containing each item in the
// list, separated by commas (e.g. 'a,b,c'):
String joinWithCommas(List<String> strings) => strings.join(',');
}
Cascades
To perform a sequence of operations on the same object, use cascades (..). We’ve all seen an expression like this:
myObject.someMethod()
// It invokes someMethod() on myObject,
// and the result of the expression is the return value of someMethod().
// Here’s the same expression with a cascade:
myObject..someMethod()
/*
Although it still invokes someMethod() on myObject,
the result of the expression isn’t the return value —
it’s a reference to myObject! Using cascades,
you can chain together operations
that would otherwise require separate statements.
For example, consider this code:
*/
var button = querySelector('#confirm');
button.text = 'Confirm';
button.classes.add('important');
button.onClick.listen((e) => window.alert('Confirmed!'));
// With cascades, the code becomes much shorter,
// and you don’t need the button variable:
querySelector('#confirm')
..text = 'Confirm'
..classes.add('important')
..onClick.listen((e) => window.alert('Confirmed!'));
๋ฐ์ํ
'Mobile > Flutter' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Flutter] Erros.. (0) | 2023.03.08 |
---|---|
[Flutter] Write your first Flutter app, part 1 (0) | 2022.06.07 |
[Flutter] (0) | 2022.05.20 |
์ฃผ์ (0) | 2020.05.18 |