From the course: Flutter Essential Training: Build for Multiple Platforms
Why you need state management - Flutter Tutorial
From the course: Flutter Essential Training: Build for Multiple Platforms
Why you need state management
- With great power comes great responsibilities. Similarly, with a lot of states comes the need for state management. Imagine an app where you fill out the profile information from one screen, and it is reflected on three other unrelated screens. Does it make sense to pass down the info via constructors from one screen to five other screens who may or may not need it. We need centralization of such states. So there is less duplicacy, and errors when states are shared between screens. For example, imagine an app that has dark and light mode enabled. Now you might change the settings from the app settings page. But the change should reflect to all other screens without explicitly sending data to these screens. For example, in our application, we might want to share the current author information throughout the app. Or we need to check if the user is logged in or not at critical stages of the app. In order to do that, let's implement a service like that for our application. So all our authentication logic is centralized. In our directory, we will create a folder called services and add our files. So here, services is the folder name, and then we can add a auth_service file. So this will be a class called AuthService. That can have methods like void loginUser and/or void logoutUser. And it can also have a method which returns the current username of the author. So it could be something like string getUserName. And suppose for now we can return the hard-coded username that we were using everywhere. So this could return something like poojab26. And now whichever widget will have the shared instance of this class will get back this value. So now maybe in our chat_page, there was some logic where we were checking if this username is equal to poojab26. Then make the alignment center-right or center-left. Here now we can do something like AuthService, make an object of it, and then we can just check, getUsername. So now we can remove the hard coding from everywhere. So wherever we were calling for poojab26, like right here as well, we could actually do the exact same thing. getUserName. However, the problem with this is that this will create a new instance of AuthService every time. And if we are trying to store something in an instance of an AuthService class, it might get overwritten by another instance when we create a new object here. And again, passing down this object around everywhere, every widget in the tree is not a scalable solution. Because we cannot always explicitly send values everywhere. Right? So this is where we would need management of the state. And of course there are community-wide solutions that can help with state management in Flutter. Here is a list of approaches that one can explore. BLoC and Riverpod is pretty famous in the community. MobX and Redux here is pretty popular among people coming from (indistinct) backgrounds. And a bunch of such community built solutions that you can experiment with.