Get_It package - Dependency Injection with Service Locator Pattern in Flutter & Dart
HTML-код
- Опубликовано: 7 сен 2024
- You want to glue together your different layers in your application, then this package is for you. The Get_It package brings an easy to use Service Locator directly into your application. The Service Locator will make it super easy to connect your app and your different layers together.
If you want to see Max when he is Live Streaming, checkout kick.com/devcafe
#GetIt #Flutter #Dart
You like our work and would like to support us, feel free to join us on Patreon.
/ flutterexp
Get_it on pub.dev
pub.dev/packag...
State Management with Get_it?
pub.dev/packag...
** Social Medias **
Website: flutter-explai...
Say hi to Max
Twitter: / flutter_exp
GitHub Max: github.com/md-...
LinkedIn: / max-weber-9889a3ba
Say hi to Mahtab
Twitter Mahtab: / mahtab_dev
GitHub Mahtab: github.com/mt-...
Even after 3years, i still find this video helpful
After searching the internet and reading medium articles to little satisfaction, this did justice to everything. Thank you very much. Subscribed!!
Glad it was helpful!
I have commented probably 4 to 5 times on RUclips. And this is the 6 times. I just wanted to say thanks for the well-structured teaching approach! Awesome teacher. I'd like the to-do style used to walk me through the video and the justification of each step. Amazing flow!
Wow, thank you so much :) I am glad you liked it!
Thanks, this is a great explanation. I struggled to understand this until I found your video. Thanks so much!
Thank you very much!
You're welcome!
I was too lazy to read the documentation 😂.. thanks you were there🥰
very brief and helpful.
good example for how to use get_it in real project, thanks
You are most welcome :) I hope it helps you along the way!
Finally!!! I was now able to understand how to use it and what is it, thanks :)
Thanks for the video Max, Could you do one please using GetIt in tests? Cheers from Sauerland
Dependency Injection Pattern left the chat
you make it super easy. and that is not fair🤣
Really cool, thanks my dear friend
thank you very much
Thanks a lot
Quality content. Thank you.
Very informative 👏
thank's very much very clear
Thanks to you :)
Thanks a lot ❤
thank you i get it
Thanks
I really like the idea of a service locator, especially that it is flutter independent as I understand. In other words, I don't need to pass something around the widgets like with provider. I would be interested, if this works well with the bloc pattern. instead of flutter_bloc, which uses provider under the hood for DI, couldn't I use get_it? Would be great if you could share your thoughts on that. Thks for the great video!
Thanks for the tutorial. I do have a question. This approach kind of goes against dependancy injection pattern, doesn't it? Having a global object that exposes all its members to anything is the opposite of DI, right? Isn't it bad?
Thanks!!
ServiceLocator seems to be "god-singleton" class. Never did i think of using 2 really bad words in programming at the same time.
Hi Ben, thanks for your feedback and your concern, maybe I did not explain it correctly but ServiceLocator is not a God Singleton, it is a registration library for singletons. Maybe I did not use it perfectly here in the video but this is a general idea. The pattern exists already quite some time but it is pretty much debated. In my research, I talked with a lot of people and figured out that there are a lot of people who love it and a lot who hate it. In my opinion, it heavily depends on your use case and how you and your team want to work.
I hope that clarifies things :) en.wikipedia.org/wiki/Singleton_pattern
Great class. I would like to see now an integration with provider for state management. Could i use 'with changeNotifier' in MemeRepo class? Thanks
That would be get_it_mixin i guess?
@@burakbirer Yes get_it_mixin will do the job!
Directly combining with provider doesn't make much sense. But state_notifier would be an option. But I highly recommend to check out the get_it_mixin which makes get_it to a full state management solution.
@@escamoteur then we absolutely need a detailed video about best practices with get_it_mixin 💙
@@FlutterExplained May we take your answer as an upcoming tutorial on get_it_mixin ? That would be great!
👍🏻
What are the use cases of this when i can normally use the normal dependency injection in Provider for example? any singleton class would do that then where is the advantage?
Very good question, of course you are able to use provider for that. It is even nearly the same idea. It is more about what tool to use for which problem. Get_it is specifically for DI while Provider should be a state management solution.
Excellent video! I do have a question though, if you can help. I was hoping get_It would be a state management solution that automatically updates the UI when changes are made like provider with notifylisteners, without the context from a widget being required. You've made it clear it's not. Are there any state management packages that can do the same thing provider can do, but without the context? Thanks in advance
You can actually use GetIT also as a state management solution nowadays you will need to use the package: get_it_mixing
pub.dev/packages/get_it_mixin
hi there, how to unregister singleton using injectable ?
Please don't mislead non-senior devs into thinking this is dependency injection. A service locator is not the same as dependency injection. SL is actually an anti pattern (the book "Dependency Injection: Principles, Practices and Patterns" explains why). For a real DI library for Dart/Flutter see Inject.
Thanks for your feedback. You are right the Service Locator Pattern (SL) can be a anti pattern because its over usage but I would not generalize it as "you should never use it because it is bad". I know also a lot of large project who overuse, and overcomplicated with a SL but there are as always some nice niche cases for it (especially in Server Dev).
Sorry if I made that not super clear in the video, maybe I can create a follow up in the next month :).
Hi.. Would like to know the config you used with your vscode? Theme, font.. thanks
I use in general Fire font with nerd icons and as a Theme I am using usually the One Dark Theme.
Hello man.
Can you make an video for getit with provider ?
Why we not just create a meme object and to use getMeme?
Great question, the problem is if you directly create the MemeRepo or MemeDomainController object and get the meme you have a strong dependency between both, this means if you want to for example create a Test you will have no chance to replace the Services with something you like. Therefore you will have to test the whole application rather then only the function.
You can find more to this topic if you search for Dependency Injection:
martinfowler.com/articles/injection.html
I have a quesion why did we not use a memeDomainCOntroler when we could directly call the meme repo as thats what is happening.
I have a question pls guide, how GetIt handles garbage collector as it registeres so many object before hand does it mean all those objects will remain in memory till app lifecycle ? or Objects will be wiped off by garbage collector if yes then when pls guide
Very good question, AFAIK if you will not clean them up they will stay in Memory you will need to create some kind of mechanism to clean them up. Lazy loading Singeltons will be created when they are needed but will also leave a footprint afterwards.
Please subtitle on you video. Im from indonesia
but..... all benefit about stateManagement is broken by setState........
Get_It is not a state management solution. Therefore it is not really broke. It is a service provider, and you have access to different layers. If you search for a state management solution, please have a look into get_it_mixins :)
@@FlutterExplained you have reason!!
mmmm more of less is like Provider... right?
@@francescofreddi4374 yes, but you don't need a Buildcontext and it offers some other nice functionality. together with the get_it_mixin it gets a full state mangement solution
I think Get X could be a better alternative because many things can be done with one package
AFAIK GetX would also solve this problem because it has a strong DI implementation. However I am personally not a big fan of GetX.
GetX is most bloatware state management for me and people are deceived because not need use context and very short lines. Remember what I say. Using GetX without context is dangerous with Responsive Layouts and what is short and simple is not mean best it will eat your errors in the background or can be hard to test and.... Even Bloc is better choose. But of course it's you choose.
@@_undecided For state management I prefer flutter block or provider. I have never used GetX in a real project. I just saw its popularity, usability and features. These options was catchy for me. If you are saying these, probably you had tested it.
@@FlutterExplained Hey, care to elaborate why you're not the biggest fan? I've seen some reservations, but GetX seemed to be a state management candidate.. Paar kurze Worte reichen :)
@@leoingson Hi there, sure. GetX is an amazing package but also a great example of a so-called golden hammer pattern. It promises everything out of the box and a lot of people will tend to use it. The problem is that you create another strong dependency between your project and GetX. So let's say I managed my State with GetX now something goes wrong in GetX and I strongly depend on the library, what happens if the main maintainer of the package is gone? Can I replace GetX easily? Do I understand the core of GetX. What happens if the GetX team drives a decision I am not willing to take in my application. Suddenly I have to remove the whole package and because it is everywhere, basically I will throw away my application.
If I would take a package that has only one single responsibility, that does not violate the Single Responsibility Principle. It is way easier to substitute it with something else. You could argue that you only want to use GetX for a single purpose, but even there if you have a team you can maybe speak for yourself, but you cannot make sure that nobody will use the offered power.
GetX remembers me a lot to jQuery, it also offered a lot and made it possible to wrap most of your JavaScript code, but boy it was horrible when we needed to get rid of it. This is just my opinion, and I will also create sooner or later a video where I talk a bit more in deep about it.