Why Riverpod is better than BLoC or GetX

Created At: 2023-12-30 05:59:57 Updated At: 2024-01-01 15:06:52

As long as you can access your ref object, internal Provider communications become super easy. In general, the providers you create in Riverpod, they are simple and pure business logic if you follow clean architecture approach.

As you can see from the above picture, I have created here AuthUser, see arrow 1. At the top you see we have used @riverpod tag to auto generate code.

This auto generated code would return authUserProvider as a provider. Some other Riverpod providers would be able to access this authUserProvider within their Providers. 

That's what we have done here as you can see with arrow 2. Here we are accessing currentUserProvider by using ref.read function. 

At the same time we are calling setUser() method and passing an object. currentUserProvider is a Provider from a class name CurrentUser. Let's take a look

Here CurrentUser provider creates currentUserProvider using @riverpod code generation tool. This currentUserProvider would be globally available by ref object. By default Riverpod create Providers as Singleton object. And they are globally available.

Because of this mechanism, we can access currentUserProvider in AuthUser class. Now, this part of Riverpod is worth praising, because with BLoC, you need to do this communication by writting a piece of code on your own.

Surprisingly, Riverpod also does another thing better than BLoC, that's updating UI. If you use the same BLoC to udpate the different parts of the UI, it won't work.

Even if you BLoC might have different states, but any state at one time, only update one part of the screen.

If your same BLoC tries to update the different parts of the screen, it just won't work. One kind of BLoC just updates one part of the screen UI. 

Thus if you update one part, other parts would lose the update. This could be very annoying.

So you have to create a tons of different BLoCs for different UI features.

But with Riverpod provider, you may update different parts of the UI. Of course you still need to create different state classes just like BLoC.

See the Riverpod clean architecture course here

Comment

Add Reviews