Riverpod or Bloc? Which One is Better?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
riverpod or block which one is better this is one of the most actively asked questions from flutter beginners comparing riverpod and block is not strictly a correct approach both of these packages can be virtually split into two parts dependency injection and State Management block uses its own extensions of the provider package for dependency injection and block or cubic classes for storing stay riverpot on the other hand is literally provider 2.0 for dependency injection and has a state Notifier package bundled with it for State Management now let's start with a simple example look at this here we are doing a simple data fetching using qubit then we change the state and display the data with the help of qubit now if we want to do exactly the same thing with riverpod how should we do it riverpod has something very similar to qubit that called State Notifier when we look at the code we see that it is almost identical with qubit State Notifier is just a qubit the implementation of both is very similar and both are based on stream controller which is a concept already proven in real world applications now let's check consuming State and widgets consuming State and widgets is very very close between block and riverpod but there are differences which we can cover in three different scenarios we're building widget on state change rebuilding widget on partial State change and reacting to State without rebuilding in rebuilding widget on state change scenarios block uses the block Builder widget to react to State changes from a block block Builder goes through the widget tree looking for the closest block of a certain type and subscribes to its state changes riverpot uses the consumer widget to do the same since we don't have our state Notifier in the tree we cannot declare the type is with block but we do have our trusty container of providers and consumer widget gives the container reference here we are explicitly subscribing to auth controller provider which handles State changes the biggest difference here is that riverpod provides an ability to use consumer on a user written widget level instead of within a widget build method while using riverpod you can use consumer widget instead of stateless widget in consumer stateful widget instead of stateful widget we're building widget on partial State change if we have a screen governed by one state object which consists of multiple widgets representing a part of that state we do not want the whole screen to reveal build when only a part of the state changes block has a block selector widget that allows you to listen to the changes of a part of the state with riverpot you have to change the code which subscribes to the state a bit but you are still using the consumer widget reacting to State without rebuilding block supplies a block listener widget which allows you to listen to changes in state and perform some action for example showing a stack bar without rebuilding the underlying widget riverpod provides similar functionality by using ref.listen instead of ref.watch wherever container reference ref is available either inside the Builder of a consumer widget or inside the build method of a consumer widget a bonus is that you can also monitor partial State change here now let's check the changing state from UI block utilizes events to send signals to State objects for it to apply business logic and make changes to the state this means that a developer needs to create a number of event classes that will be processed by the block object in riverpod you need to access the state object from the UI via reading it from the container so you will be using ref.read to call functions inside of it that make changes to the state this function is generally used for one time time actions like pressing a button now let's check the dependency injection block uses block provider for dependency injection block provider is a flutter widget which provides a block to its children it is used as a dependency injection widget so that a single instance of a block can be provided to multiple widgets within a subtree and in riverpot we can wrap our root widget with a provider scope provider scope is a widget that stores the state of all the providers we create both riverpod and block are excellent and it is impossible to say which one is better use of them only depends on the scale of the project and the developer's personal interest now tell us which one do you prefer to use and make the sure hit the Subscribe button to get next video foreign
Info
Channel: Flutter Guys
Views: 30,703
Rating: undefined out of 5
Keywords: flutter tutorial, bloc state management flutter, bloc state management, bloc state management flutter شرح, riverpod flutter, riverpod 2.0, riverpod flutter tutorial, riverpod state management flutter, riverpod statenotifier provider, riverpod state management, flutter bloc tutorial, flutter bloc state management, flutter app tutorial, flutter 2023 course, flutter state management, flutter riverpod, flutter provider, flutter course, flutter tutorial for beginners
Id: EPVKdverFuw
Channel Id: undefined
Length: 3min 43sec (223 seconds)
Published: Tue Feb 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.