C와 Swift 언어에 비교하며 Dart의 차이점 정리
```dart
var name = "string";
name = 1; // Error
var hello;
hello = "string";
hello = 1; // 가능
```
```dart
String name = "string";
```
```dart
dynamic name = "string";
name = 1;
```
String? name = "string";
name = null;
final과 const 키워드가 붙은 변수는 초기화 이후 수정 불가
final과 const를 붙이면 var 키워드 생략 가능
차이점
final은 빌드타임에 값을 가지고 있지 않아도 됨
const는 빌드타임에 값을 가지고 있어야 함.(즉 런타임에 동적할당되는 것들은 사용못한다는 뜻? 객체 등)
final String finalName;
name = "finalName";
name = "finalName2"; // Error
const String constName; // Error(빌드시에 값을 안가지고 있으므로)
name = "constName";
double? value = 1.0;
// value가 null이면 3.0을 대입
value ??= 3.0;
String name = "string";
number is String; // 타입이 맞는지?
number is! String; // 타입이 다른지?
List<int> list = [1, 2, 3];
list.length;
list.add(3); // 끝에 추가
list.remove(3); // index가 가장 낮은 1개만 지워짐
Map<String, String> dict = {
'1' : 'one',
'2' : 'two',
'3' : 'three'
};
dict.remove('5');
dict.remove('3'); // 이렇게 삭제해야함
Set<String> s = {
'one', 'two', 'three'
};
s.contains('one');
// 대부분의 기능이 Swift Array, Dictionary, Set와 같음.
enum Status {
approve,
pending,
reject
}
Status status = Status.pending
```dart
void main() {
test(1,2);
}
void test(int x, [int y = 10, int z = 20]) {
print(x);
print(y);
print(z);
}
```
```dart
void main() {
test(0, x: 1, y: 2, z: 3);
}
void test(int k, {
required int x,
required int y,
int z = 3 // optional parameter
}) {
print(x);
print(y);
print(z);
}
```
void main() {
print(test(0, x: 1, y: 2, z: 3));
}
int test(int k, {
required int x,
required int y,
int z = 3 // optional parameter
}) => k + x + y + z;
```dart
typedef GlobalFunc = int Function(int x, int y);
void main() {
GlobalFunc g = test1;
g = test2;
g = test3;
print(confirm(1,2,g));
}
int test1(int x, int y) => x + y;
int test2(int x, int y) => x - y;
int test3(int x, int y) => x * y;
int confirm(int x, int y, GlobalFunc f) => f(x,y);
```
```dart
void main() {
var a = (){
print("a");
};
Function b = (){
print("b");
};
dynamic c = () {
print("c");
};
}
```
class Idol {
String name;
List<String> l;
Idol(String name, List<String> l)
: this.name = name,
this.l = l;
// or
Idol(this.name, this.l);
}
```dart
void main() {
Idol.fromList(['g','h']);
}
class Idol {
String name;
List<String> l;
Idol(this.name, this.l);
// fromList라는 이름의 생성자
Idol.fromList(List<String> members)
: this.name = members[0],
this.l = members;
}
```
void main() {
const Idol("test", ["test"]);
}
class Idol {
final String name;
final List<String> l;
const Idol(this.name, this.l);
Idol.fromList(List<String> members)
: this.name = members[0],
this.l = members;
}
생성자 앞에 const를 붙이고 멤버를 같게 만든 두 객체는 같은 객체로 판단됨void main() {
Idol(name: "아이돌1", cnt: 5);
BoyGroup("아이돌2", 10, "hi");
}
class Idol {
String name;
int cnt;
Idol({
required this.name,
required this.cnt
});
void printIdol() {
print("idol");
}
}
class BoyGroup extends Idol{
String greeting;
BoyGroup(
String name,
int cnt,
String greeting
):
this.greeting = greeting,
super(
name: name,
cnt: cnt
);
}
void main() {
BoyGroup("아이돌2");
}
abstract class IdolInterface {
String name;
IdolInterface(this.name);
void sayGreeting();
}
class BoyGroup implements IdolInterface{
String name;
BoyGroup(this.name);
void sayGreeting() {
}
}
void main() {
Lecture<String, String> lecture = Lecture("id", "math");
}
class Lecture<T, X> {
final T id;
final X name;
Lecture(this.id, this.name);
}
cascade
동일한 객체에서 연속적인 작업을 할때 ..로 표시하여 사용한다.
void main() {
Student student = Student();
student
..age = 13
..name = "name2"
..printName();
}
class Student {
int age = 12;
String name = "test";
List<String> friends = ["test2", "test3"];
Student() { }
void printName() {
print(name);
}
}