Learn Dart Language in one day

2 Min. Read
Sep 4, 2019

 Build Mob App Android iOS Dart Flutter Introduction

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.

1
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.

1
2
3
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:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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();
  // ...
}

You might also like

Hidden View Helper Methods In Rails 

ShivaShiva Bhusal (CTO) 2 Min. Read Oct 1, 2019
 rails ruby views helpers

Summary of the post.
Read More..

Learn the awesome language Dart for Desktop, Mobile app development in just one day. You must have working knowledge of any language first.
Read More..

Find all the Provinces with districts with all Municipalities in Nepal.
Read More..

The purpose of this library is to combine features of `google map` such as map, marker and corresponding info window rendering to a single API call. This simplifies implementing google map into your project while not having to remember all the associated API calls provided by it. Just include the script tag into your project and initialize and object of `ideaMap` class with appropriate parameters while rendering the map. While instantiating the `ideaMap` class, there are certain parameters that you need to pass according to your requirement. Let’s go over them, Here, we’ve passed in an object while initializing the map.
Read More..




Write To Me

Hire me shiva bhusal
We'll never share your email with anyone else.
I'll never share your email with anyone else.