Shiva Bhusal
Shiva's Blog

Follow

Shiva's Blog

Follow

Learn Dart Language in one day

Shiva Bhusal's photo
Shiva Bhusal
·Sep 4, 2019·

3 min read

Play this article

I am assuming you have good knowledge of language like c, java, js or c++ before you continue. Otherwise, It will take you months to speak in dart.

Introduction

Variables

Vars can be typed and dynamic.

var name = 'Bob';

Since the data is a String, the type of name will be String. You wont be able to assign other type to it later.

Futures with Async and Await keywords

In software development you need to wait for some processes to complete, and there is no point in moving on until the process gives you the result that you need to process. Such operations are called synchronous operation.

On contrary, some operations like IO from internet or FileSystem, you are not sure when it will complete. So, you do not wait for it to complete; you(main exec thread) rather move on and give the responsibility of doing the job to a async function which await for the operation to complete.

Key terms:

  • synchronous operation: A synchronous operation blocks other operations from executing until it completes.
  • synchronous function: A synchronous function only performs synchronous operations.
  • asynchronous operation: Once initiated, an asynchronous operation allows other operations to execute before it completes.
  • asynchronous function: An asynchronous function performs at least one asynchronous operation and can also perform synchronous operations.

Futures

These are basically instances of class Future. They are what a async function returns as the result. A future can have two states i.e. completed or uncompleted.

  • Uncompleted:- default value of a future object as soon as its created or operation is executed.
  • Completed:- when async operation is done, the state changes to Completed.

When dart interpreter sees, a function returning a future, it starts a separate thread and start executing it.

Completed Futures

A future can have return data of predefined-type.

Future<String> getStringFromAPI() async{
  return(Future.delayed(Duration(seconds: 4), () => 'Large Latte'))
}

In the example above, the Future instance is expect to return string object when the future becomes completed. Though the intermediate/interim return type of the function is Future.

Future<void> means that, on execution the async operation returns nothing or the data returned has no use in this context.

Working with futures: async and await

To define an async function, add async before the function body: async keyword is necessary before the function body to use the await keyword. await keyword does not work in sync functions. It only works in async functions.

Example:-

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

Future<String> getUserOrder() async {
  // Imagine that this function credits the price of the order from payment processor like `Stripe`
  var val = await Future.delayed(Duration(seconds: 4), () => 'Large Latte');
  return val;
}

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

Example with sync main() function:-


Future<void> createOrder() async{
  var order = await getUserOrder();
  print('Success!! Your order is: $order');
}

Future<String> getUserOrder() async {
  // Imagine that this function credits the price of the order from payment processor like `Stripe`
  var val = await Future.delayed(Duration(seconds: 4), () => 'Large Latte');
  return val;
}

main (){
  createOrder();

  // doSomethingImportant();
  // ...
}
 
Share this