flutter/조금 매운맛 (중급)
[flutter2.0] 8강 - 1 | Future, async, await 이해하기
lgvv
2021. 8. 17. 15:46
✅이번 시간에는 Future, async, await에 대해서 알아보자.
플러터를 공부하면서 느낀것이 전반적으로 RxSwift와 비슷한 느낌을 갖는다는 것이다.
그럼 한번 다시 보도록 할까?
코드 작성은 플러터 프로젝트의 - test - widget_test.dart에 작성한다.
✅가장 기준이 되는 코드
import 'dart:io';
void main() {
showData();
}
void showData() {
startTask();
accessData();
fetchData();
}
void startTask() {
String info1 = '요청 수행 시작';
print(info1);
}
void accessData() {
String info2 = '데이터에 접속중';
print(info2);
}
void fetchData() {
String info3 = '잔약은 8500원 입니다.';
print(info3);
}
✅개선 코드 1
import 'dart:io';
void main() {
showData();
}
void showData() {
startTask();
accessData();
fetchData();
}
void startTask() {
String info1 = '요청 수행 시작';
print(info1);
}
void accessData() {
Duration time = Duration(seconds: 3);
sleep(time);
String info2 = '데이터에 접속중';
print(info2);
}
void fetchData() {
String info3 = '잔약은 8500원 입니다.';
print(info3);
}
// 결과값
요청 수행 시작
- 지연 3초 -
데이터에 접속중
잔약은 8500원 입니다.
✅개선 코드 2
import 'dart:io';
void main() {
showData();
}
void showData() {
startTask();
accessData();
fetchData();
}
void startTask() {
String info1 = '요청 수행 시작';
print(info1);
}
void accessData() {
Duration time = Duration(seconds: 3);
if (time.inSeconds > 2) {
sleep(time);
String info2 = '데이터에 접속중';
print(info2);
} else {
String info2 = '데이터에 접속중';
print(info2);
}
}
void fetchData() {
String info3 = '잔약은 8500원 입니다.';
print(info3);
}
딜레이 시간이 2초보다 크면 3초간 재운다음에 실행 그렇지 않으면 바로 실행
✅개선 코드 3
import 'dart:io';
void main() {
showData();
}
void showData() {
startTask();
accessData();
fetchData();
}
void startTask() {
String info1 = '요청 수행 시작';
print(info1);
}
void accessData() {
Duration time = Duration(seconds: 3);
if (time.inSeconds > 2) {
//sleep(time);
Future.delayed(time, (){
String info2 = '데이터에 접속중';
print(info2);
});
} else {
String info2 = '데이터에 접속중';
print(info2);
}
}
void fetchData() {
String info3 = '잔약은 8500원 입니다.';
print(info3);
}
// 결과
요청 수행 시작
잔약은 8500원 입니다.
데이터에 접속중
✅개선 코드 4
import 'dart:io';
void main() {
showData();
}
void showData() {
startTask();
String account = accessData();
fetchData(account);
}
void startTask() {
String info1 = '요청 수행 시작';
print(info1);
}
String accessData() {
String account = "null";
Duration time = Duration(seconds: 3);
if (time.inSeconds > 2) {
//sleep(time);
Future.delayed(time, (){
account = '데이터 처리 완료';
print(account);
});
} else {
String info2 = '데이터에 접속중';
print(info2);
}
return account;
}
void fetchData(String account) {
String info3 = '잔액은 $account 입니다.';
print(info3);
}
// 결과값
요청 수행 시작
잔액은 null 입니다.
데이터 처리 완료
✅개선 코드 5
import 'dart:io';
void main() {
showData();
}
void showData() async{
startTask();
String account = await accessData();
fetchData(account);
}
void startTask() {
String info1 = '요청 수행 시작';
print(info1);
}
Future<String> accessData() async{
String account = "null";
Duration time = Duration(seconds: 3);
if (time.inSeconds > 2) {
//sleep(time);
await Future.delayed(time, (){
account = '데이터 처리 완료';
print(account);
});
} else {
String info2 = '데이터에 접속중';
print(info2);
}
return account;
}
void fetchData(String account) {
String info3 = '잔액은 $account 입니다.';
print(info3);
}
// 결과값
요청 수행 시작
- 지연 -
데이터 처리 완료
잔액은 데이터 처리 완료 입니다.