Topics covered: Bloc Cubit River Pod Builder Future asycn Splash Screen Shared Preferences async * Future Builder Stream Builder
Bloc It has three folders: bloc : Contains the business logic components. state : Contains the state classes representing the various states of the application. event : Contains the event classes that trigger state changes . When an event is triggered, it initializes a state. The bloc contains functions that handle these events and manage the state transitions.
Cubit Folders : bloc : Contains the business logic components. state : Contains the state classes representing the various states of the application. Purpose : Used as a simpler alternative to Bloc when Bloc brings too much complexity. Emit Function : Used in Cubit to emit new states and notify the UI of state changes.
Riverpod Functionality : Acts like multi-threading, providing enhanced state management capabilities. An enhanced version of Provider, offering more features. Use Cases : Ideal for applications requiring instant state changes, such as chat apps. Suitable for scenarios where request and response handling need to be synchronized.
Builder Used to conditionally wrap other widgets based on a condition. Usage : Conditional Rendering : The Builder widget is used to conditionally render parts of the UI based on certain conditions .. Dialog Creation : By using the Builder widget to wrap a widget tree that includes an AlertDialog or SimpleDialog , developers can easily create dialogs that pop up over the main UI. State Management : The Builder widget can be used to manage state within a specific part of the widget tree, allowing for more granular control over state changes and updates.
Future In Flutter, a Future function is one that runs first in a sequence of functions. For example, if there are five functions and the fifth function is a Future function, it will run first.
async An async function in Dart is a function that performs asynchronous operations. It contains only one Future function and uses the await keyword to pause execution until the Future completes.
SplashScreen A splash screen in Flutter is a screen that appears for a short period when an app is launched. It includes an initState method that runs first when the app is opened, allowing for initial setup. The Future.delayed function can be used within initState to introduce a delay before loading the main content, useful for showing branding or introductory animations.
Shared Preferences Shared Preferences in Flutter is a storage solution for small amounts of data, similar to how WhatsApp backs up chat history. Storage : It utilizes the device's storage, including libraries like sqflite and Hive. As it deals with a single entity, there's no need for complex loading operations.
async * An async * function in Dart is used to create a stream of values asynchronously. It can contain multiple Future functions, allowing for the generation of multiple values over time.
FutureBuilder Functionality : Used in Flutter to load and display data asynchronously. Requires specifying the data type expected from the Future, especially important when using Firebase. Builder Method : The builder method of FutureBuilder must return a widget. It has two parameters: context and snapshot. Snapshot Parameter : Provides the state of the connection, indicating if the data is loaded or if there's an error. Helps manage UI based on the state of the asynchronous operation.
StreamBuilder Used in Flutter for building UI based on stream data, suitable for real-time updates like chat apps. Preferred over FutureBuilder for chat apps to avoid frequent loading . Usage : Specifies the stream using stream: APIClass.getData ();, with datatype required for Firebase. In the builder method, var data = snapshot.data ; is used to access the data. Data can be directly used or processed using a model approach, which is considered better for organizing and managing data.