728x90
๋ฐ์ํ
about using this in a constructor
this.propertyName ์ ์งง๊ณ ๊ฐ๋จํ๊ฒ constructor์ values์ ๊ฐ์ ํ ๋นํ ๋ ์ฌ์ฉํ๋ค.
class MyColor {
int red;
int green;
int blue;
MyColor(this.red, this.green, this.blue);
}
final color = MyColor(80, 80, 128);
์ ์๋ ์ฝ๋์์ this.์ class ๋ณ์์ ๊ฐ์ ๋ฃ๊ฒ ๋ค๋ ๊ฒ์ ์๋ฏธํ๋ค.
์ด๋ฌํ ๋ฐฉ์์ named parameters์์๋ ์ฌ์ฉ๋๋ค.
using this in named parameters
class MyColor {
...
MyColor({required this.red, required this.green, required this.blue});
}
final color = MyColor(red: 80, green: 80, blue: 80);
์ ์ฝ๋๋ MyColor์ parameters์ this๋ฅผ ์ฌ์ฉํ ์์ด๋ค.
์ฌ๊ธฐ์๋ required๊ฐ ์ฌ์ฉ๋์๋๋ฐ, int values๋ null์ด ๋ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค.
- ๋ง์ฝ, ๊ฐ์ ์ง์ ํ์ง ์์๋ ๋๋ parameter๋ผ๋ฉด required๋ฅผ ์๋ตํด๋ ๋๋ค.
Question
MyColor([this.red = 0, this.green = 0, this.blue = 0]);
// or
MyColor({this.red = 0, this.green = 0, this.blue = 0});
์ฒซ ๋ฒ์งธ ์ฝ๋๋ ๋ฆฌ์คํธ?
๋ ๋ฒ์งธ ์ฝ๋๋ property names๊ฐ parameters๊ฐ ๋ ์ ์์์ ๋ณด์ฌ์ฃผ๋ ์.
Code example
class MyClass {
final int anInt;
final String aString;
final double aDouble;
// Create a constructor here.
}
Solution
class MyClass {
final int anInt;
final String aString;
final double aDouble;
MyClass(this.anInt, this.aString, this.aDouble);
}
์ฐธ๊ณ ์ฌ์ดํธ: https://dart.dev/codelabs/dart-cheatsheet#initializer-lists
728x90
๋ฐ์ํ
'๐ง flutter' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Flutter] FractionallySizedBox class ์ ๋ฆฌ (0) | 2022.07.06 |
---|---|
[Flutter] FittedBox Class ์ ๋ฆฌ (0) | 2022.07.06 |
[Flutter] CustomSingleChildLayout ์ ๋ฆฌ (0) | 2022.07.06 |
[Flutter] Initializer lists ์ ๋ฆฌ (0) | 2022.07.06 |
[Flutter] Exceptions ์ด๋!? (0) | 2022.07.06 |