Flutter/2.0

Flutter Future, async, await 이해하기

lgvv 2021. 8. 17. 15:46

Flutter Future, async, await 이해하기

 

Flutter에서 비동기 처리(Future, async, await)는 RxSwift와 비슷한 흐름으로 이해할 수 있음.

이번 시간에는 순차적 실행 → 지연 → 비동기 처리 → await 사용 순으로 발전하는 예제를 통해 학습

 

 

실습을 통해 하나하나 바꿔가면서 알아보자.

 

1. 기본 동기 실행 

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

 

딜레이 시간이 2초보다 크면 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);
    String info2 = '데이터에 접속중';
    print(info2);
  } else {
    String info2 = '데이터에 접속중';
    print(info2);
  }
  
}

void fetchData() {
  String info3 = '잔약은 8500원 입니다.';
  print(info3);
}

 

 

 

개선 코드 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);
}

// 결과값
요청 수행 시작
- 지연 - 
데이터 처리 완료
잔액은 데이터 처리 완료 입니다.