8/8/2023 0 Comments Flutter providerThis is why I use get_it for dependency injection and only use the StateManagement functionality from the Provider package, for now. Just add the property and get it from the locator. When I inject a new service into a model or a service I don't have to change anything in the locator file. registerLazySingleton ( ( ) = > LoginModel ( ) ) // Usage var api = locator ( )Įasy to read and easy to understand. registerLazySingleton ( ( ) = > AuthenticationService ( ) ) registerLazySingleton ( ( ) = > Api ( ) ) I don't like that.Ĭompare the above to GetIt locator = GetIt ( ) The other thing I don't like is when new properties are introduced that requires injection, instead of only modifying the file where the property is required I have to not only create a new ProxyProvider but also update the code where I inject the new service, every time. Imagine the setup and proxy providers required just for that. This is injection for one service only, I have services that are single purpose, so I use up to 5 in one model and in my manager classes. If you know anything about my tutorials or the way I write code, I don't want a lot of code, I keep things simple. Then we use another ProxyProvider to get the AuthenticationService and inject it into the Login Model's property. Then we use a proxy provider to get access to the Api and do property injection on the Authentication service when we register it. The Authentication Service depends on the Api so get that in there first. ProviderBuilder : (_, login, child ) = > ChangeNotifierProvider. custom (īuilder : (_, auth, previous ) = > (previous ? LoginModel ( ) ). api = api ,ĭispose : (_, auth ) = > auth. dispose ( ), ), ProxyProvider (īuilder : (_, api, previous ) = > (previous ? AuthenticationService ( ) ). For example here's how you would inject the AuthenticationService into the LoginModel using a MultiProvider and the ProxyProvider. I'm not a fan of more code with no added benefits just to keep things "pure"). I had a discussion with Remi (The creator of Provider) very informative and it showed me that with the coming updates and the ProxyProvider, all the dependency injection can be done with provider, but in my opinion, it's not going to be maintainable (For me. There have been a few comments on the architecture, and all of the feedback has been extremely valuable. We'll start with the "objections" and then move onto some implementation updates. Each view has it's own model, any widget with logic would have the same, we have a base view that provides a state enum to listen too and we use services to define all our business logic. In the guide we setup the provider architecture (using it only for it's state management) and using get_it for dependency injection. Here is my code to do this: if (context.read().This architecture guide will be a continuation of the first guide. My problem is that when a name is selected, I only want the tile to be added to the list once and then the number of times selected counter to increase with each press. and the tiles are being added to the list. these tiles are then displayed elsewhere in the app. I have an empty, dynamic list that accepts tiles in a change notifier and when the user makes selections in another part of the app, these selections are added to the list as tiles that show the name they selected as well as how many times they selected it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |