๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Mobile/Flutter

Dart cheatsheet codelab

๋ฐ˜์‘ํ˜•

https://dart.dev/codelabs/dart-cheatsheet

 

Dart cheatsheet codelab

Interactively learn (or relearn) some of Dart's unique features.

dart.dev

 

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