Blogs By Shankar
Dart
Dart
  • Dart Tutorial by Dès Vu Technologies
  • hello
  • Dart Collection
    • collectionInDart
  • File Handeling
    • fileHandelingInDart
  • Functions
    • functionParameters
    • Types of Functions in Dart
    • Functions in Dart
    • Annonymous Function
      • AnnonymousFunction
    • Arrow Function
      • arrowFunctions
    • BuiltinFunctions
      • importantBuiltinFunctions
      • mathFunction
  • OOP With Dart
    • encapsulation
    • oopWithDart
    • Generic In Dart
      • generic
    • constructor
      • constructor
      • factory constructor
        • factoryConstructor
      • initializer list constructor
        • Initializer List Constructor:
    • async dart
      • asyncAndAwait
        • asyncAndAwait
      • future
        • future
      • streams
        • streams
  • Sync and Async dart
    • syncAndAsyncDart
  • controlFlow
    • controlFlow
  • dataTypes
    • Dart Built-In Data Types
    • TypeConversion
    • String
      • stringOperations
      • string_jnterpolation
    • operators
      • Operators In Dart
  • operators
    • operators
  • user_input
    • userInput
  • variablesAndConstants
    • scopeInDart
    • variableTypesInDart
    • variables
Powered by GitBook
On this page
  1. Sync and Async dart

syncAndAsyncDart

Dart supports both synchronous and asynchronous programming to handle operations that may take some time to complete, such as I/O operations or network requests. Understanding the difference between synchronous and asynchronous programming is crucial for efficient and responsive Dart applications.

Synchronous Programming:

In synchronous programming, tasks are executed sequentially, one after the other, in a blocking manner. Each task must wait for the previous task to complete before it can start. Synchronous code is easy to read and understand because it follows a linear execution flow.

Here's an example of synchronous Dart code:

void main() {
  print('Task 1');
  print('Task 2');
  print('Task 3');
}

In this code, "Task 1" is executed first, followed by "Task 2," and finally "Task 3." Each task completes before the next one begins.

Asynchronous Programming:

In asynchronous programming, tasks can be executed concurrently, allowing non-blocking execution. Asynchronous code is used when you want to perform operations that may take time (e.g., reading from a file, making a network request) without blocking the main program's execution. Dart provides two primary mechanisms for asynchronous programming:

  1. Futures: A Future represents a potential value or error that will be available at some point in the future. It allows you to execute code asynchronously and receive the result when it's ready.

    void main() {
      print('Task 1');
      Future.delayed(Duration(seconds: 2), () {
        print('Task 2 (asynchronous)');
      });
      print('Task 3');
    }

    In this code, "Task 1" and "Task 3" are executed immediately, and "Task 2 (asynchronous)" is executed after a delay of 2 seconds without blocking the main program.

  2. Async/Await: The async and await keywords allow you to write asynchronous code that looks similar to synchronous code, making it easier to read and maintain.

    Future<void> doTasks() async {
      print('Task 1');
      await Future.delayed(Duration(seconds: 2));
      print('Task 2 (asynchronous)');
      print('Task 3');
    }
    
    void main() {
      doTasks();
    }

    In this code, the await keyword is used to pause execution until the asynchronous operation is completed, making the code appear sequential.

Asynchronous programming is essential for responsive applications, especially when dealing with I/O-bound or network-bound tasks, to avoid blocking the user interface or other critical operations. Dart's asynchronous features make it relatively easy to work with asynchronous code in a readable and maintainable way.

PreviousSync and Async dartNextcontrolFlow

Last updated 1 year ago