Complete BLoC with Clean Architecture (group chat) Discount !! E-commerce App With Backend Source Code Video and Voice Chatting App Firebase Chatting App Source Code Complete Gym App BLoC State Management Source Code Complete Study App Buy Ticket Booking App Source Code Buy Travel App With Backend Source Code Complete Chat App Udemy Course Special Offer Discount !! Online Learning Course App (BLoC) Online Learning Course App (Riverpod) Online Learning Course App (Getx) Discount !! Shopping App (Provider) Cool Flutter Game Flutter Nodejs Chat App Flutter Nodejs Api And Firebase Chat App Riverpod Task Management App
Singletons are great way to optimize your app performance. If you use Singleton classes, you may optimize memory, since you don't need to create the same instance again and again.
One of the great example of using Singletons could be your home page. If you home page involves data calling or depends on restful api, then you must implement Singleton classes to optimize performance.
With Singleton classes, you may create instance only once in your app, and call the same instance again to get the data. So it helps stop recreating object and stops reloading data.
Analyze a problem
If your home page is on bottom navigation bar, and every time you switch between tabs, then your data would be loading again and again. This is extremely bad for user experience since, you need to load the data each time you click on home page screen.
Here on the button tap on the navigation bar, data gets reload again and again.
This is also bad for server, since you need to retreive the same data again and again. To stop reloading data
, we may use Singleton classes.
With BLoC
or Getx
, you may need to create Singleton classes on your own to optimize your app performance. Even with Riverpod at first glance, it may look like you need to create your own Singleton classes.
Solution
If you use Riverpod latest version it makes super easy to create a controller and mark it as Singleton class behavior.
In my case I have used Riverpod generator to generate async notifier provider. This helped me to load data from the network based on the repo.
Here you see we have @riverpod
annotation which generates code and since we have FutureOr in our build() method return, it returns data asynchronously.
First it does not look like it can help solve our problems. But it does. We can use Riverpod's keep alive property during code generation.
By default, in latest Riverpod, providers are destroyed when they are not needed
. So when you go to a new tab, the related async notifier provider is destroyed. Since this is destroyed, when you go back to your home page tab, this provider is auto created and hence the data is reloaded from the server.
So overcome this problem, we need to use
@Riverpod(keepAlive: true)
If we have this instead of @riverpod annotation
, the providers won't be destroyed. That means you when you tap between tabs, we don't recreate the providers, hence flutter won't call the api to reload the data.
That also means your auto generated providers also work as Singleton class.
A Riverpod Provider() is a lazy, globally identified singleton, with the added advantage that it can be subscribed for changes, and overridden for testing.
That means if you use Riverpod, you don't need to create Singletons.