<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Q. Aftab Alam Flutter Developer on Medium]]></title>
        <description><![CDATA[Stories by Q. Aftab Alam Flutter Developer on Medium]]></description>
        <link>https://medium.com/@mobilecoderz.flutter?source=rss-be5064ff8ac9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*gL_s31ZPCg_Grnmc</url>
            <title>Stories by Q. Aftab Alam Flutter Developer on Medium</title>
            <link>https://medium.com/@mobilecoderz.flutter?source=rss-be5064ff8ac9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 17:32:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mobilecoderz.flutter/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Understanding Synchronous and Asynchronous Programming in Flutter]]></title>
            <link>https://medium.com/@mobilecoderz.flutter/understanding-synchronous-and-asynchronous-programming-in-flutter-15267948820b?source=rss-be5064ff8ac9------2</link>
            <guid isPermaLink="false">https://medium.com/p/15267948820b</guid>
            <dc:creator><![CDATA[Q. Aftab Alam Flutter Developer]]></dc:creator>
            <pubDate>Thu, 15 Feb 2024 10:23:37 GMT</pubDate>
            <atom:updated>2024-02-15T10:23:37.021Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Synchronous VS Asynchronous Programming in Flutter/Dart" src="https://cdn-images-1.medium.com/max/1024/1*KoZ5geLBAVAYg27btyGKgA.jpeg" /><figcaption>Sync vs Async</figcaption></figure><p>In the realm of Flutter development, understanding the concepts of synchronous and asynchronous programming is crucial for creating responsive and efficient applications. Synchronous and asynchronous programming techniques allow developers to manage tasks and resources effectively, providing a smoother user experience.</p><p>In this blog post, we will delve into the differences between synchronous and asynchronous programming in Flutter, explore their implications, and provide examples to illustrate their usage.</p><p>**Synchronous Programming**</p><p>Synchronous programming refers to the traditional style of execution where tasks are performed sequentially, one after the other. In synchronous programming, each operation blocks the execution until it completes, and the program waits for the current task to finish before moving on to the next one.</p><p>Let’s take a look at an example of synchronous programming in Flutter:</p><pre>void printNumbersSync1() {<br> print(&quot;Start print till - 5&quot;);<br> for (int i = 1; i &lt;= 5; i++) {<br> print(i);<br> // Simulate a delay<br> sleep(Duration(seconds: 1));<br> }<br> print(&quot;End print till - 5&quot;);<br>print(&quot;Start print till - 8&quot;);<br> for (int i = 1; i &lt;= 8; i++) {<br> print(i);<br> // Simulate a delay<br> sleep(Duration(seconds: 1));<br> }<br> print(&quot;End print till - 8&quot;);<br>}<br><br>  void sync(){<br>    print(&quot;Start sync 1 &quot;);<br>    printNumbersSync1();<br>    print(&quot;End sync 1&quot;);<br>  }</pre><p>In this code snippet, the `printNumbersSync1()` function prints numbers synchronously with a simulated delay of one second between each print statement. Each iteration of the loop waits for the delay to complete before moving on to the next iteration.</p><p>**Asynchronous Programming**</p><p>Asynchronous programming, on the other hand, allows tasks to be executed concurrently, without blocking the main execution thread. Asynchronous operations do not wait for each other to complete, enabling the program to perform other tasks while waiting for certain operations to finish.</p><p>Let’s consider an example of asynchronous programming in Flutter:</p><pre>Future&lt;void&gt; printNumbersAsync1() async {<br> print(&quot;Start print till - 5&quot;);<br> for (int i = 1; i &lt;= 5; i++) {<br> print(i);<br> // Simulate a delay without blocking the main thread<br> await Future.delayed(Duration(seconds: 1));<br> }<br> print(&quot;End print till - 5&quot;);<br>print(&quot;Start print till - 8&quot;);<br> for (int i = 1; i &lt;= 8; i++) {<br> print(i);<br> // Simulate a delay without blocking the main thread<br> await Future.delayed(Duration(seconds: 1));<br> }<br> print(&quot;End print till - 8&quot;);<br>}<br><br><br>void async(){<br>    print(&quot;Start Async1&quot;);<br>    printNumbersAsync1();<br>    print(&quot;End Async1&quot;);<br>  }</pre><p>In this code snippet, the `printNumbersAsync1()` function prints numbers asynchronously using `Future. delayed`. The `await` keyword is used to pause the execution of each iteration of the loop until the specified delay completes. Meanwhile, the main execution thread remains unblocked, allowing other tasks to be performed concurrently.</p><p>**Choosing Between Synchronous and Asynchronous Programming**</p><p>When developing Flutter applications, choosing between synchronous and asynchronous programming depends on the nature of the tasks and the desired behaviour of the application. Here are some guidelines to consider:</p><p>- **Synchronous Programming**: Use synchronous programming for tasks that require sequential execution or when the order of operations is critical. Synchronous programming is suitable for tasks such as simple calculations, data processing, and UI updates that do not involve significant delays.</p><ul><li>**Asynchronous Programming**: Use asynchronous programming for tasks that involve I/O operations, network requests, or long-running computations. Asynchronous programming helps improve application responsiveness by preventing the main execution thread from being blocked, allowing the user interface to remain interactive while waiting for operations to complete.</li><li>The full Code is here.</li></ul><pre>import &#39;dart:io&#39;;<br>import &#39;package:flutter/material.dart&#39;;<br><br>void main() {<br>  runApp(MyApp());<br>}<br><br>class MyApp extends StatefulWidget {<br>  const MyApp({Key? key}) : super(key: key);<br><br>  @override<br>  State&lt;MyApp&gt; createState() =&gt; _MyAppState();<br>}<br><br>class _MyAppState extends State&lt;MyApp&gt; {<br>  @override<br>  void initState() {<br>    super.initState();<br>  }<br>//----------------------------------------------------<br>  void sync(){<br>    print(&quot;Start sync 1 &quot;);<br>    printNumbersSync1();<br>    print(&quot;End sync 1&quot;);<br><br>    print(&quot;Start sync 2 &quot;);<br>    printNumbersSync2();<br>    print(&quot;End sync 2&quot;);<br>  }<br>//----------------------------------------------------<br>  void printNumbersSync1() {<br>    print(&quot;Start print till - 5&quot;);<br>    for (int i = 1; i &lt;= 5; i++) {<br>      print(i);<br>      // Simulate a delay<br>      sleep(Duration(seconds: 1));<br>    }<br>    print(&quot;End print till - 5&quot;);<br><br>    print(&quot;Start print till - 8&quot;);<br>    for (int i = 1; i &lt;= 8; i++) {<br>      print(i);<br>      // Simulate a delay<br>      sleep(Duration(seconds: 1));<br>    }<br>    print(&quot;End print till - 8&quot;);<br>  }<br>//----------------------------------------------------<br>  void printNumbersSync2() {<br>    print(&quot;Start print till - 10&quot;);<br>    for (int i = 1; i &lt;= 10; i++) {<br>      print(i);<br>      // Simulate a delay<br>      sleep(Duration(seconds: 1));<br>    }<br>    print(&quot;End print till - 10&quot;);<br>  }<br>//----------------------------------------------------<br>  void async(){<br>    print(&quot;Start Async1&quot;);<br>    printNumbersAsync1();<br>    print(&quot;End Async1&quot;);<br><br>    print(&quot;Start Async2&quot;);<br>    printNumbersAsync2();<br>    print(&quot;End Async2&quot;);<br>  }<br>//----------------------------------------------------<br>  Future&lt;void&gt; printNumbersAsync1() async {<br>    print(&quot;Start print till - 5&quot;);<br>    for (int i = 1; i &lt;= 5; i++) {<br>      print(i);<br>      // Simulate a delay without blocking the main thread<br>      await Future.delayed(Duration(seconds: 1));<br>    }<br>    print(&quot;End print till - 5&quot;);<br><br>    print(&quot;Start print till - 8&quot;);<br>    for (int i = 1; i &lt;= 8; i++) {<br>      print(i);<br>      // Simulate a delay without blocking the main thread<br>      await Future.delayed(Duration(seconds: 1));<br>    }<br>    print(&quot;End print till - 8&quot;);<br>  }<br>//----------------------------------------------------<br>  Future&lt;void&gt; printNumbersAsync2() async {<br>    print(&quot;Start print till - 10&quot;);<br>    for (int i = 1; i &lt;= 10; i++) {<br>    print(i);<br>      // Simulate a delay without blocking the main thread<br>      await Future.delayed(Duration(seconds: 1));<br>    }<br>    print(&quot;End print till - 10&quot;);<br><br>  }<br><br>  @override<br>  Widget build(BuildContext context) {<br>    return MaterialApp(<br>      home: Directionality(<br>        textDirection: TextDirection.ltr,<br>        child: Scaffold(<br>          body: Container(<br>            height: MediaQuery.of(context).size.height,<br>            width: MediaQuery.of(context).size.width,<br>            color: Colors.yellowAccent,<br><br>            child: Padding(<br>              padding: const EdgeInsets.all(80.0),<br>              child: Center(<br>                child: Column(<br>                  children: [<br>                    ElevatedButton(onPressed: (){sync();}, child: Text(&quot;Synchonous&quot;)),<br>                    ElevatedButton(onPressed: (){async();}, child: Text(&quot;Asynchonous&quot;)),<br>                  ],<br>                ),<br>              ),<br>            ),<br>          ),<br>        ),<br>      ),<br>    );<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h6Tm_P92lbdpv38C8ZuuvA.png" /></figure><p><strong><em>THE BELOW IS THE RESULT FOR THE SYNC PROGRAMMING.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/495/1*OiaT8lIQOPDwBFhzU_J-0Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*riDP2YkpUsEfuvBTQ4bUhg.png" /></figure><p><strong><em>THE BELOW IS THE RESULT FOR THE ASYNC PROGRAMMING.</em></strong></p><figure><img alt="ASYNC PROGRAM RESULT" src="https://cdn-images-1.medium.com/max/1024/1*frbynnYNibSZX4Kd0FtWeA.png" /><figcaption>ASYNC PROGRAMM</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*75eVRNLTQsQRJY-8VIJLNg.png" /></figure><ul><li>*Conclusion**</li></ul><p>In conclusion, synchronous and asynchronous programming are fundamental concepts in Flutter development, each with its own advantages and use cases. Understanding when and how to use synchronous and asynchronous techniques is essential for building efficient and responsive Flutter applications. By leveraging synchronous and asynchronous programming effectively, developers can create high-performance applications that deliver a seamless user experience.</p><p>In the provided Flutter code, we demonstrated the differences between synchronous and asynchronous programming through two sets of functions: `printNumbersSync1()` and `printNumbersAsync1()`. By running the application and observing the output, developers can gain a deeper understanding of how synchronous and asynchronous programming impact the execution flow and performance of Flutter applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=15267948820b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>