Home > Net >  Dart Understanding Futures
Dart Understanding Futures

Time:09-02

when I run the below code snippet,I can't "Your order is: Large Latte".Instead I get "Your order is: Instance of '_Future".So what am I doing wrong?

Future<String> createOrderMessage() async{
  var order =  await fetchUserOrder();
  return 'Your order is: $order';
}

Future<String> fetchUserOrder() =>
   
    Future.delayed(
      const Duration(seconds: 2),
      () => 'Large Latte',
    );

void main() {
  print(createOrderMessage());
}

CodePudding user response:

You have to await the first method call. So change to this:

void main() async {
  print(await createOrderMessage());
}

If not, the main method will not wait for the Future to complete and instead just print that createOrderMessage() is an instance of a Future.

  • Related