Thứ Tư, 18 tháng 9, 2019

await and Future.delayed trong flutter


void createOrderMessage () async {
  print('Run createOrderMessage...');
  var order = await getUserOrder();
  print('Your order is: $order');
}

Future<String> getUserOrder() {
  // Imagine that this function is more complex and slow.
  return Future.delayed(Duration(seconds: 30), () => 'Large Latte');
}

main() async {
  await createOrderMessage();
  countSeconds(4);
 
}

// You can ignore this function - it's here to visualize delay time in this example.
void countSeconds(s) {
  print('Run countSeconds()...');
  for( var i = 1 ; i <= s; i++ ) {
      Future.delayed(Duration(seconds: i), () => print(i));
   }
}

Ket Qua :

Run createOrderMessage... Your order is: Large Latte Run countSeconds()... 1 2 3 4

Không có nhận xét nào:

Đăng nhận xét

How to reload app in flutter

  Phoenix . rebirth (context);