| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 |
- Happy moment
- 데이터
- flutter_secure_storage
- fetch
- 정리
- flutter
- riverpid
- StateNotifierProvider
- 가공
- StateProvider
- FutureProvider
- StreamProvider
- Flutter 상태관리
- Today
- Total
This Week's Record
Dart 기초 본문
코드는 무조건 위에서부터 아래로 작동한다.
변수 타입
정수 int
+ - * /
실수 double
+ - * /
bool 맞다 틀리다 (true, false)
글자 String
print(name+name2) + 가능
print('${name}') 문자열안에 변수를 넣고 ㅅㅍ을때
var vs dynamic
var는 한번 선언하면 그 타입으로 지정
dynamic는 타입을 요리조리 바꿀 수 있음
nullable non-nullable
null : 아무런 값도 없다.
String -> non-nullable
String? -> nullable
name! -> name은 null이 아니다.
final vs const
둘다 값을 한번 선언하면 변경불가
둘다 var 생략가능 (final String name == final name)
final은 buildtime의 값을 몰라도 되고, const는 절대적으로 buildtime의 값을 알아야함⭐️
operation
% 나머지
a=2
a ++ -> 3
a -- ->1
a+=3 ->5
a-+3 ->-1
a ?? = 3.0; -> a가 null이면 오른쪽값으로 바꿔라
a > b
a < b
a >= b
a <= b
a == b
a != b
int number1 = 1;
number is int //true
number is! int //false
&& -> and (둘다 ture여야함)
|| -> or (둘중 하나만 만족해도 됨)
List
List<String> shinee = ['종현', '태민', '기범', '민호', '온유' ];
List<int> numbers = [1, 2, 3, 4, 5, 6];
리스트의 내용물 인덱스로 뽑아내기
index // 0부터 시작
shinee[0] //종현
리스트 내용물의 길이
shinee.length //5
리스트에 내용 추가하기 & 지우기
shinee.add('샤월'); //['종현', '태민', '기범', '민호', '온유', '샤월']
shinee.remove('샤월'); //['종현', '태민', '기범', '민호', '온유']
리스트의 특정값 인덱스값 알아내기
shinee.indexOf('태민') // 1
Map
// Key / Value
Map<String, String> dictionary = {
'Harry Potter' : '해리포터',
'Ron Weasley' : '론 위즐리'
}
Map<String, bool> isHarryPotter = {
'Harry Potter' : true,
'Ron Weasley' : true,
'Ironman' : false,
}
Key로 Value값 가져오기
isHarryPotter['Ironman'] // false -> key에 해당하는 value값을 가져올 수 있다. 단, value에 대한 key값은 가져올 수 없다.
Map에 Map형식으로 추가하기
isHarryPotter.addAll({
'Spiderman' : false,
});
// {HarryPotter: true, ... , Spiderman: false}
Key와 Value로 추가하기
isHarryPotter['Hulk'] = false; // {HarryPotter: true, ... , Spiderman: false, Hulk: false}
Key로 Value값 바꾸기
isHarryPotter['Spiderman'] = true; // {HarryPotter: true, ... , Spiderman: true, Hulk: false}
Key로 값 지우기
isHarryPotter.remove('Hulk'); // {HarryPotter: true, ... , Spiderman: true}
Key값만 가져오기
isHarryPotter.keys // (HarryPotter, Ron Weasley, Ironman, Spiderman)
Value값만 가져오기
isHarryPotter.values // (true, false)
Set
중복처리가 자동으로 됨
final Set<String> names = {
'Floa',
'Floa',
'John',
'Judy'
}
// {Floa,John,Judy}
names.add('Flor');
names.remove('Tuly');
names.contains('John');
조건문
if문
if(조건){
조건이 true일때 실행할 문
} else {
조건이 false일때 실행할 문
}
if(조건){
조건이 true일때 실행할 문
} else if(조건2) {
조건2가 true일때 실행할 문
} else {
둘다 false일때 실행할 문
}
switch
swtich(number % 3){
case 0 :
print('나머지가 0입니다');
break;
case 1 :
print('나머지가 1입니다');
break;
default :
print('나머지가 2입니다');
break;
}
반복문
loop
for(int i = 0; i < 10; i++){
print(i);
}
int total = 0;
List<int> numbers = [1,2,3,4,5,6];
for(int i = 0, i> numbers.length; i++){
total += numbers[i];
}
print(total); // 21
total = 0;
for(int number in numbers){
total += number;
}
print(total); // 21
while
int total = 0;
while(total < 10){
total += 1;
}
break
//루프를 빠져나옴
int total = 0;
while(total < 10){
total += 1;
if(total == 5){
break;
}
}
continue
//특정루프 일부만 건너띔
for(int i = 0; i<10; i++){
if(i == 5){
continue;
}
print(i);
}
// 0,1,2,3,4,6,7,8,9 // 5만 건너띔!
enum
//enum은 몇가지 타입만 있을때 사용. 오타가 나거나 하는 예측불가한 에러를 줄이기에 효과적.
enum Status{
approved, //승인
pending, //대기
rejected, //거절
}
void main(){
Status status = Status.pending;
if(status == Status.approved){
print('승인입니다.');
}else if(status == Status.pending){
print('대기입니다.');
}else{
print('거절입니다.');
}
}
함수
void main() {
addNumbers();
}
// parameter, argument - 매개변수
// optional parameter - 선택적 매개변수 : [대괄호]로 감싸면 됨
// named parameter - 이름이 있는 파라미터, 순서가 중요하지 않다 -> addNumbers({required int x, int y=20,int z = 30 ,}) : {중괄호}로 감싸고 필수매개변수는 required키워드 넣기
// 값 반환 : 사실상 아래는 addNumbers 앞에 void가 생략. 앞에 int addNumbers 라고 하면 int 를 반환하는 함수가 되고 return int값을 해줘야함.
addNumbers(int x, int y = 20, {required int z = 30}){
int sum = x + y + z;
if(sum % 2 == 0) {
print('$sum은 짝수입니다.');
} else {
print('$sum은 홀수입니다.');
}
}
int addNumbers(int x, int y = 20, {required int z = 30})=> x+y+z;
-> 리턴값으로 x+y+z을 반환
typedef
typedef Operation = int Fuction(int x, int y, int z);
ㄴ 요런식으로 함수의 타입을 미리 지정해둘 수 있음(?)
아래는 예시.
void main() {
int result = calculate(40, 50, 60, sub);
}
// signature
typedef Operation = int Fuction(int x, int y, int z);
int add(int x, int y, int z);
int sub(int x, int y, int z);
int calculate(int x, int y, int z, Operation operation){
return operation(x, y, z);
}
'Flutter > 기초&문법' 카테고리의 다른 글
| [Flutter] Dart 문법 요약 (0) | 2022.08.27 |
|---|