Asynchronous programming in Dart
Async/await Dart

As a developer, you might be knowing what concurrency exactly means. Concurrency is the ability to execute more then one task at a given time, means Executing multiple tasks simultaneously, By running multiple task concurrently will enhance the performance & responsiveness of the application. Let’s check out concurrency in dart programming language.

Asynchronous Programming in dart

Basically to perform non-blocking I/O operations & execute task concurrently without affecting or blocking the main thread developer make use of asynchronous programming model that let’s us to run multiple task simultaneously. Async programming in dart will enable responsive & to build scalable dart application.

void main() {
 print("Start");
  
  Future.delayed(Duration(seconds:2),()=>{
    print("Delayed Task 2 second")
  });
  
  print("End");
}

Output

Start
End
Delayed Task 2 second

Here, Start and End print statement will execute immediately and then after 2 second Delayed Task 2 second will get printed.


Future & Async/await

To work with asynchronous operation in dart we have Future function with async/await. Developer can make use of Future object to represent asynchronous computations and async/await syntax to write asynchronous code sequentially.

void main() async {
 print("Start");
  
  await delayedTask(); // waits here for task to complete
  
  print("End");
}

Future<void> delayedTask() async {
  await Future.delayed(Duration(seconds:2),()=>{
    print("Delayed Task 2 second")
  });
}

Isolates in dart

There is another concept been provided by dart i.e. Isolates an lightweight concurrent execution unit that run in seperate memory spaces, with enables a the true parallel processing in dart and isolation of state. Isolate communicate via message passing making them ideal for CPU-bound tasks.

Check out this article on Isolate iin dart – Flutter Isolate – Run Task in Background – MultiThreading for example

Here is simple example on Isolate

import 'dart:isolate';
void main() async {
 ReceivePort receivePort = ReceivePort();
  await Isolate.spawn(isolateFunction, receivePort.sendPort);
  receivePort.listen((data)=>{
    print('Received: ${data}')
  });
}

void isolateFunction(SendPort sendPort){
  sendPort.send("Message fron Isolate");
}

Working with Streams in dart

Stream-based concurrency model is avalilable in dart language that allows developers to process data asynchronously handling streams of data efficiently. Streams in dart facilitate development of event-driven application.

check out this article on streams Flutter Dart Stream Basic Example – Fetch Crypto Currency API Data

Basic Code Example on Streams in dart

import 'dart:async';
void main() async {
 StreamController<int> controller = StreamController<int>();
  
  controller.stream.listen((data){
    print('Received: $data');
  });
  
  controller.add(1);
  controller.add(3);
  controller.add(5);
}

Dart Concurrency model

Dart’s concurrency model revolves around isolates event loops and asynchronous programming primitives. leveraging isolates & async programming, developer can built a super responsive and scalable & high performance application in flutter.

Managing Concurrent Tasks

To manage concurrent task in Dart programming language will involves a proper handling of asynchronous operation, mangaing errors.

Techniques like error handling, cancellation & timeouts are essentials for robust concurrent programming.

Conclusion

Concurrency operation/task in dart programming language are basically used to execute multiple tasks simutaneously using async nature without blocking the main thread.