Async Coding With Dart: Futures

MIB Coder
3 min readOct 30, 2019

--

What you are going to learn: What is Future? How to create Future? How to use Future in Dart code? How to use Future in Flutter UI?

To read the latest version of this article click here: https://mibcoder.com

Future in Dart

What is Future?

A future represents the result of an asynchronous operation, and can have two states: 1- uncompleted, 2- completed (Completing with a value,Completing with an error).

1- Uncompleted

When asynchronous function called, it returns an uncompleted future. After that future keep waiting for the function’s asynchronous operation to completed(with value or error).

2- Completed

i- Completing with a value: If asynchronous operation successfully completed, It will return value.

ii- Completing with an error: In case it fails, It throw error.

How to create Future?

Number of way to create Future in Dart, Here are some of them:

i- Create Future by Future class constructor:

//Create Future by Future class constructor
Future<int> createFutureMethodOne() {
Future<int> future = Future(workDoInFuture);
return future;
}
int workDoInFuture(){

//Do any work here it can be Async/Isolate
print('workDoInFuture');

Random rng = Random();
if(rng.nextBool()){
return rng.nextInt(100);
}else{
throw("There is some error");
}

}

ii- Create Future by Completer

//Create future by Completer
Future<int> createFutureMethodTwo(){
Completer completer = Completer<int>();

//Do any work here it can be Async/Isolate
Random rng = Random();
if(rng.nextBool()){
completer.complete(rng.nextInt(100));
}else{
throw("There is some error");
}

return completer.future;
}

iii- Create Future by marking method “async”

//Create future by marking method "async"
Future<int> createFutureMethodThree() async {

//Do any work here it can be Async/Isolate
Random rng = Random();
if(rng.nextBool()){
return rng.nextInt(100);
}else{
throw("There is some error");
}
}

iv- Create Future by Future class factory constructors(Its mostly helpful in unit tests)

//Create future class factory constructors(Its mostly helpful in unit tests)
Future<int> createFutureMethodFour(){

Random rng = Random();
if(rng.nextBool()){
int value= rng.nextInt(100);

//First e.g.
// return Future.value(value);

//Second e.g.
// return Future.error('There is some error');

//Third e.g.
return Future.delayed( Duration(seconds: 3),(){
return value;
});

}else{
throw("There is some error");
}
}

Here is complete example:

How to create Future

How to use Future in Dart code?

In Dart code two ways to use Futures: 1- Using Future class methods then/catchError/whenComplete, 2- Using async/await with try/catch/finally

1- Using Future class methods then/catchError/whenComplete

//Use Future by then/catchError/whenComplete
void useFutureMethodOne(){

Future<String> future =getFuture();

future.then((String onValue){
print('On success : $onValue');
})
.catchError((Object onError){
print('On error : $onError');
})
.whenComplete((){
print('On success/error its commpeted');
});
}

2- Using async/await with try/catch/finally

//Use Future by async/await/try,catch,finally
void useFutureMethodTwo()async{

Future<String> future =getFuture();

try{
String onValue=await future;
print('On success : $onValue');
}on Exception catch( onError){
print('On error : $onError');
}finally{
print('On success/error its commpeted');
}

}

Here is complete code:

How to use Future in Dart code

How to use Future in Flutter UI?

FutureBuilder Widget use to render based on current state of Future(Uncompleted, complete with value , complete with error)

FutureBuilder takes two parameters :

1- future: put method name which is providing future

2- builder: need to implement method which returning Widget to show based on state of Future that can be Uncompleted, complete with value , complete with error

Here is complete code:

How to use Future in Flutter UI

--

--