Book Image

Managing State in Flutter Pragmatically

By : RAHUL AGARWAL, Waleed Arshad
Book Image

Managing State in Flutter Pragmatically

By: RAHUL AGARWAL, Waleed Arshad

Overview of this book

Flutter is a cross-platform user interface (UI) toolkit that enables developers to create beautiful native applications for mobile, desktop, and the web with a single codebase. State management in Flutter is one of the most crucial and complex topics within Flutter, with a wide array of approaches available that can make it easy to get lost due to information overload. Managing State in Flutter Pragmatically is a definitive guide to starting out with Flutter and learning about state management, helping developers with some experience of state management to choose the most appropriate solutions and techniques to use. The book takes a hands-on approach and begins by covering the basics of Flutter state management before exploring how to build and manipulate a shopping cart app using popular approaches such as BLoC/Cubit, Provider, MobX, and Riverpod. Throughout the book, you'll also learn how to adopt approaches from React such as Redux and all its types. By the end of this Flutter book, you'll have gained a holistic view of all the state management approaches in Flutter, and learned which approach is the best solution for managing state in your app development journey.
Table of Contents (14 chapters)
1
Section 1:The Basics of State Management
4
Section 2:Types, Techniques, and Approaches
8
Section 3:Code-Level Implementation

Importance of a state in an application

The state is the core building block of your application. It defines the overall behavior of your app, from the beginning of your user journey to the time the user closes/terminates your application. Every change the user sees is a state of its own, and you need to make sure that your user sees what you intend to show. You also need to make sure that with every possible user interaction, the application shows valid states. It should not show something that you or the user don't expect to see (an exception, a red screen, an unexpected or unhandled error, and so on).

Different states in a large application

Since we now know that every user interaction creates a new state for an application, a complete functional application can have hundreds of states. In order to keep track of and manage every state, it is important to understand what role a state plays inside your application. Here are some examples of states that get updated by user interactions in a simple login page consisting of two text fields and a button:

  • The user enters a correct email and password – A new state that navigates the user to some other screen
  • The user enters the wrong email and password – A new state showing an error
  • The user presses the login button without entering anything – A state that says that the user has to fill in both the required fields
  • The user presses the login button with only one filled-in field – A state indicating the field that needs to be filled in
  • The internet gets disconnected – A state that shows a pop-up dialog for no internet connection

The preceding example was of one single login page with the three simplest forms of UI components, and you saw how many states were extracted from it. When your application gets bigger, there can be a lot of states to manage. Therefore, studying states and knowing everything about them is as important as building an entire application.

We have seen how important states are and how they help us get the most out of our application. We have also seen the immense importance of states in a larger application with many states. Let's now understand what state management is and how we can manage states in our application through different techniques (the actual point of this book).