Future In Dart

Future In Dart

In dart, the Future represents a value that is not yet available. It is used to represent a potential value, or error, that will be available at some time in the future. Future represents the result of an asynchronous operation and can have 2 states.

State Of Future

  • Uncompleted
  • Completed

Uncompleted

When you call an asynchronous function, it returns to an uncompleted future. It means the future is waiting for the function asynchronous operation to finish or to throw an error.

Completed

  • It can be completed with value or completed with error. Future<int> produces an int value, and Future<String> produces a String value. If the future doesn’t produce any value, then the type of future is Future<void>.
Info

Note: If the asynchronous operation performed by the function fails due to any reason, the future completes with an error.

Example

main() {
print("Start");
getData();
print("End");
}

void getData() async{
  String data = await middleFunction();
  print(data);
}

Future<String> middleFunction(){
  return Future.delayed(Duration(seconds:5), ()=> "Hello");
}

Show Output
In the above example, First, it prints Start, secondly it prints End, and after 5 seconds Hello will be printed.