* 비동기
- 아래 printWithDelay함수에서 await를 빼고 실행시키면 Future.delayed와 print가 동시에 실행되는 듯 하고, await을 넣고 실행시키면 delay가 적용된 후에 print가 실행된다.
//Future : An object representing a delayed computation.
//async : async 함수 본문 앞에 키워드 사용하여 비동기로 표시
//await : async 내에서 작동하는 기능.
Future<void> printWithDelay(Duration delay, String message) async {
await Future.delayed(delay);
print(message);
}
Future<void> printWithDelay2() async {
var order = await printWithDelayReturn();
print(order + " Method");
}
Future<String> printWithDelayReturn() async {
return Future.delayed(Duration(seconds: 3), () => '3 seconds');
}
void test() {
print('Hello Test');
}
void main() {
printWithDelay2();
printWithDelay(Duration(seconds: 1), '1 seconds Method');
test();
}
//결과
1 seconds Method
Hello Test
3 seconds Method
* then : 앞 실행이 완료 될 때 호출 할 콜백을 등록
const one = Duration(seconds: 1);
Future<void> printWithDelay(String message) {
return Future.delayed(one).then(((_) {
print(message);
}));
}
void test() {
print("Hello Test");
}
void main() {
printWithDelay("1 seconds Method");
test();
}
//결과
Hello Test
1 seconds Method
* promise ? await ? : async await과 promise는 같은 개념
- promise는 error가 어디서 발생했는지 알기 어렵고, await는 try/catch로 에러 핸들링 가능. (await 사용 권장)
void test(){}
void test2(){}
void test3(){}
test().then(test2());
test3();
- 다음과 같이 test() 함수가 실행 완료된 후 then(test2())이 호출 됨 ▶ promise의 then()
- 비동기식이면 위와 같이 test3() 함수 먼저 호출이 가능. 동기식이면 test()와 test2() 실행 완료 후에 test3() 호출됨.
같이 보면 좋은 포스팅
https://cording-cossk3.tistory.com/85
[Flutter] Dart 문법 (async, Future, await, then)
* 비동기 - 아래 printWithDelay함수에서 await를 빼고 실행시키면 Future.delayed와 print가 동시에 실행되는 듯 하고, await을 넣고 실행시키면 delay가 적용된 후에 print가 실행된다. //Future : An object representing
cording-cossk3.tistory.com
https://cording-cossk3.tistory.com/86
[Flutter] Dart 문법 (getter, setter)
* getter, setter class Rectangle { double left, top, width, height; Rectangle(this.left, this.top, this.width, this.height); //Define two calculated properties : right and bottom. double get right => left + width; set right(double value) => left = value -
cording-cossk3.tistory.com
https://cording-cossk3.tistory.com/87
[Flutter] Dart 문법 (json)
* 클래스 직렬화 (Json) - Flutter는 간단한 json 인코더와 디코더가 내장된 dart:convert 라이브러리 가짐 - fromJson : map 구조에서 새로운 User 객체 생성하기 위한 생성자 - toJson : User 객체를 map 구조로 변
cording-cossk3.tistory.com
https://cording-cossk3.tistory.com/88
[Flutter] Dart 문법 (skip, take)
* skip, take - skip : 첫 번째 (count) 요소 제외한 모든 요소 제공하는 iterable 반환, 즉 count개 요소 제외한 모든 요소 - take : 이 iterable의 첫 번째 요소 (count) 개의 lazy iterable 반환, 즉 iterable의 첫 번째 요
cording-cossk3.tistory.com
https://cording-cossk3.tistory.com/89
[Flutter] Dart 문법(??=, ??, null 인식 연산자, cascade, ..) #flutter operater ?? #flutter null #dart ..
* null 인식 연산자 : ??=, ?? * cascade (..연산자) : 자바의 builder 패턴과 유사. 객체 레퍼런스 변수 없이 바로 값 대입 가능 class Animal { var name = "dog"; var age; var sound; } main() { var a = 3; // ??= : 변수가 현재
cording-cossk3.tistory.com
'프로그래밍 > Flutter-Dart' 카테고리의 다른 글
[Flutter] Dart 문법 (json) (0) | 2021.03.15 |
---|---|
[Flutter] Dart 문법 (getter, setter) (0) | 2021.03.15 |
[Flutter] Dart 문법 (abstract, implement) (0) | 2021.03.15 |
[Flutter] Dart 문법 (상속) (0) | 2021.03.15 |
[Flutter] Dart 문법 (class, ?, !, ?.) (0) | 2021.03.15 |
댓글