본문 바로가기

Flutter (with 코딩셰프)

[2024.02.07] 플러터(flutter) 조금 매운맛 강좌 08 | Future, async, await 이해하

<< 소스 코드 >>

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='';
  Duration time = Duration(seconds: 3);

  if(time.inSeconds > 2){
    //sleep(time);
    await Future.delayed(time, (){
      account = '8,500만원';
      print(account);
    });
  } else {
    String info2 = '8,500만원';
    print(info2);
  }

  return account;
}

void fetchData(String account) {
  String info3 = '잔액은 $account 입니다.';
  print(info3);
}

 

<< 출력 화면 >>