12/30/2023 0 Comments Riverpod v2![]() The syntax and feel of riverpod are much better than bloc’s, and once you learn it, it becomes a joy to write the code.īut not everything’s perfect. The dependency injection is of a much higher standard than bloc’s. It provides a similar level of documentation, and many people are starting to pick it up in their real-world applications. To wrap up, riverpod is a great state management package that, on many occasions, is simpler than bloc. You can mock different layers, like in the example above, and test your widgets this way, but it’s imperfect as you don’t completely control what you’re displaying.Ĭreating a wrapper class for StateNotifier and setting the desired state directly there would be another solution. And here is the problem - you can’t mock state notifiers’ state (or at least at the point of writing this article, more info here). At AppUnite, we usually try to mock the lowest layer we can, so we ensure to test only the UI and not everything in between. Widget and golden tests are another story because they depend on your approach. Although it doesn’t provide an additional library for tests like bloc, nothing more than the standard flutter test library and mockito/mocktail should be enough. Testing the state notifier is pretty straightforward. If they are not initialized by the point of accessing them, they will be initialized then. It’s also worth mentioning that when creating a provider, you get access to the ref, with which you can access other dependencies like in the example above. You can do it by adding a modifier to the StateNotifierProvider like this: ![]() Usually, you don’t want to keep the state of the particular widget through its lifetime, and you wish to auto-dispose it once it’s closed. ProviderScope works exactly like RepositoryProvider, it creates objects when they’re listened to.īecause ProviderScope is localized on top of the widgets tree, disposing of the dependencies is not easy. To make it work, you need to wrap your app with ProviderScope, and that’s it. Riverpod replaces BlocProviders and RepositoryProviders with, you guessed it, “providers.” It consists of the same issues that riverpod tries to solve. Dependency Injectionīloc, highly inspired by a provider, uses a simple DI. You can also wrap futures, streams, or even singular values into providers and use them in your widgets with the same WidgetRef logic, which can turn out pretty handy, especially if you want to test these. listen method that listens to the state’s changes without rebuilding the widget, just without nesting another widget in your widget tree. With it, you can more easily decide which parts of the UI should be rebuilt or used inside a StatefulWidget. There’s also Consumer as a widget that you can use inside your widget tree (similar to BlocBuilder). * ConsumerWidget acts just as a StatelessWidget, with a handy way to access your providers with WidgetRef. Ok, enough talking, let’s see the widget: ![]() ![]() Well, little to no difference, riverpod offers similar. Ok, but what about widget level? What’s the difference there? The implementation of both is very similar, and both are based on StreamController, an already proven concept in real-world applications. When you look at the code, it’s nearly identical: Riverpod has something very similar to Cubit, called StateNotifier. Ok, let’s start with a simple fetch data example Cubit like this: So it’d be better if you had at least basic knowledge of bloc before reading this. Note that I won’t be explaining how bloc works. It solves many problems of provider package and adds additional features that the provider wasn’t capable of doing, but how does it compare to a bloc? How does it manage the state? How about dependency injection? Is it testable in the same way as a bloc is? I’ll try to answer all of these while going in-depth on how riverpod works compared to bloc. Lately, Riverpod, a “better version of provider” created by no one else than the original creator of provider Remi Rousselet, has been a hot topic in the Flutter community. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |