기초에 대한 정리
JavaScript 와 매우 비슷하고, 다른 언어들과 크게 다르지 않은 모습이다.
변수
참 / 거짓 | bool |
정수 | int |
실수 | double |
문자열 | String |
Null | null |
연산자
산술 | +, -, *, /, % .... |
비교 | ==, <=, >=, != .... |
논리 | &&, ||, ?? |
할당 | =, *=, -=, /= ... |
클래스
객체 만들기 위한 Template
필드, 메서드, 생성자로 구성
필드 | 클래스 내부 선언된 데이터 |
메서드 | 클래스 내부 선언된 기능 |
생성자 | 클래스 인스턴스 생성시 사용 |
함수/메서드
리턴타입 함수명 (매개변수) {
return 리턴
}
ex)
int sum(int a, int b) {
return a+b;
}
분기문
if else if else
if (condition1) {
// 실행 1
}
else if (condition2) {
// 실행 2
}
else {
// 실행 3
}
if case 패턴매칭
if (pair case [int x, int y]) return Point(x, y);
pair 의 값이 배열 int x, int y이 형식이라면 Point(x,y)를 반환한다
라는 if case 문도 있다
switch
일반 switch 문과 동일하지만
break를 명시하지 않아도 된다
continue, throw, return 사용가능 continue는 switch문 내에 다른 케이스로 점프용
switch (command) {
case 'OPEN':
executeOpen();
continue newCase; // Continues executing at the newCase label.
case 'DENIED': // Empty case falls through.
case 'CLOSED':
executeClosed(); // Runs for both DENIED and CLOSED,
newCase:
case 'PENDING':
executeNowClosed(); // Runs for both OPEN and PENDING.
}
패턴매칭을 이용한 switch
token = switch (charCode) {
slash || star || plus || minus => operator(charCode),
comma || semicolon => punctuation(charCode),
>= digit0 && <= digit9 => number(),
_ => throw FormatException('Invalid')
};
마지막 _ 는 default를 뜻한다
반복문
for
for ... in ...
다른 언어와 동일
for (final Candidate(:name, :yearsExperience) in candidates) {
print('$name has $yearsExperience of experience.');
}
추가로 이렇게 패턴매칭으로 구현 가능
while
do ... while
다른 언어와 동일
예외처리
try catch finally
다른 언어와 동일
try on catch finally
try {
breedMoreLlamas();
} on OutOfLlamasException {
// A specific exception
buyMoreLlamas();
} on Exception catch (e) {
// Anything else that is an exception
print('Unknown exception: $e');
} catch (e) {
// No specified type, handles all
print('Something really unknown: $e');
}
on catch는 특정 Exception을 잡을 수 있다
rethrow
void misbehave() {
try {
dynamic foo = true;
print(foo++); // Runtime error
} catch (e) {
print('misbehave() partially handled ${e.runtimeType}.');
rethrow; // Allow callers to see the exception.
}
}
void main() {
try {
misbehave();
} catch (e) {
print('main() finished handling ${e.runtimeType}.');
}
}
main 에서 misbehave 를 실행하고
misbehave는 런타임 오류를 발생시켜 catch로 간다
catch 에서 rethrow를 발생시켜 호출함수인 main에도 catch로 넘어가게 된다
결국 출력값으로

이렇게 나오게 된다
Dart Language
Introduction to Dart
A brief introduction to Dart programs and important concepts.
dart.dev
'프로그래밍 > Dart & Flutter' 카테고리의 다른 글
[기초] Flutter State 관리 (0) | 2024.09.20 |
---|---|
[기초] Flutter Widget, Layout (0) | 2024.09.19 |
[기초] Dart Language 는 무엇인가! (1) | 2024.09.08 |