Asynchronous programming - A multithreaded model that’s most applicable to networking and communications
Asynchronous programming - is a non-blocking architecture
Multiple related operations can run concurrently without waiting for other tasks to complete.
Non-blocking architecture - it doesn’t stop further executions while one or more operations are in progress.
Async is multi-threaded, which means operations and programs run in parallel
Async is non-blocking, which means it can send multiple requests to a server
Sync is single-threaded in which one operation or program will run at a time
Sync is blocking, as it can only send the server one request at a time and will wait for the request to be answered by the server
Async increases the throughput in which multiple operations can run at the same time
Sync is slower and more methodical
Async programming is ideal for development projects with large iterations because it doesn't follow a sequence. It keeps the development moving forward.
When to use async?
● Used in programming independent tasks.
● Responsive UI.
Async offers advantages for users because it enhances their experience by decreasing the lag time and waiting time. This translates to faster, more seamless flows.
Sync offers advantages for developers because it is simple and easier to code. It is also well-supported by all programming languages as the default programming method. No learning curve for sync programming.
In Flutter, we need asynchronous code to fetch data over a network, write in a database, or read from a file
Asynchronous computations usually provide a Future as a result.
Dart use the async and await keywords.
Future is a result of an asynchronous computation.
Two states of future:
Uncompleted
Completed.
Uncompleted - Calling an asynchronous function returns this future. This future waits for the function’s asynchronous operation to finish or throw an error.
Completed - Asynchronous operation succeeds, the future terminates with a value
Future syntax in Dart:
Future<T>, T can be of any data type or void.
A Future<T> instance produces a value of type T. If a future doesn’t produce any usable value, then the future’s type is void
If asynchronous operation performed by the function fails for any reason, the future is completed with an error.
To define an async function, add async before the function body.
Await keyword works only in async functions
State - an information that can be read when the widget is built and changed over a lifetime of the app
setState() - allows us to set the properties of the state object that triggers the redraw of the UI
State of an app is everything that exist in memory when the app is running. This includes the app’s assets, variables, UI, animation state, textures, and fonts.
State management - the most popular and necessary processes in the lifecycle of an application
Flutter is declarative
Flutter builds its user interface to reflect the current state of your application
Types of Conceptual States
Ephemeral State
App State
Ephemeral State - the UI state or local state. It is the state you can neatly contain in a single widget
Ephemeral State - No need to use state management techniques on this kind of state just use a StatefulWidget and the method Setstate().
App State - This is when you want to share across many parts of your app. It is also called a shared state.
There is also no clear-cut, universal rule to distinguish whether a particular variable is ephemeral or app state.
Provider package - an easy-to-use package that wraps around InheritedWidgets that makes it easier to use and manage.
Provider package - Widget listens to changes in the state and update it as soon as they are notified