Flutter Asynchronous programming | async | await | Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial we will study asynchronous programming using flutter and also demonstrate an example asynchronous programming is a programming Paradigm where tasks are executed concurrently allowing the program to continue executing other tasks without waiting for the completion of a particular operation it enables non-blocking execution which improves the responsiveness and efficiency of the program I will give a real life example to understand synchronous programming imagine you are cooking dinner in your kitchen while you wait for the water to boil you decide to chop vegetables instead of waiting for the water to boil before starting to chop you choose to perform both tasks concurrently this way you maximize the use of your time and finish dinner preparation more efficiently in this scenario boiling water represents a a synchronous task because you need to wait for it to complete before moving on to the next step unlike chopping vegetables which you can do simultaneously now we are going to start the example program I will explain more details as we go along let's begin by starting a blank project open the pubp do yaml file and include the HTTP dependency then press Pub get the h HTTP package in flutter is a powerful tool that allows you to make HTTP requests to communicate with web servers or apis in this example we fetch data from a remote server using the HTTP package declare a future variable in asynchronous programming INF flutter a future represents a potential value or error that will be available at some point in the future it allows you to perform operations asynchronously and receive the result when it's ready in this example we fetch data from a remote server using the HTTP package declare a future variable here we are fetching data from a remote server that data will be stored in this variable we fetch the data in the initstate function create a fetch data function it is an asynchronous function so use the async keyword here then we receive the response from the remote website in this case the await keyword is used to pause the execution of an asynchronous function until a future completes and returns a value then we check what is included on this remote it's a list of posts in Json format which includes post ID title and body of the post next we verify if the response variable contains data if there is data we decode it using the Json decode function then we return the data as a map object currently we are extracting only the title value then proceed to the display part we utilize the future Builder to construct the UI future Builder is a widget in flutter that streamlines the handling of asynchronous data it enables you to create UI components based on the asynchronous snapshot of a future as it evolves over time require two parameters the first parameter passes our underscore date future variable while the second one is the Builder where we design based on the data check if the connection is okay or if there are any errors e e e e run the app retrieve the data from the remote server and display it in our app this concludes our lesson on asynchronous programming using flutter keep practicing thank you for watching
Info
Channel: True Coders
Views: 65
Rating: undefined out of 5
Keywords:
Id: nhJic1XoI9E
Channel Id: undefined
Length: 9min 22sec (562 seconds)
Published: Sat Apr 20 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.