Suragch
2 min readApr 23, 2021

--

I would drop GetIt if I saw a good alternative.

As you said, you could certainly create an abstract `DataBaseService` and implement it with `DataBaseFirebaseService`. But if you have to do `final db = DataBaseFirebaseService();` everywhere that you want to use it, I see at least two problems:

1. Your app still knows about the implementation details. You haven't hidden them. If you want to switch to another database service then you would have to go through your code everywhere and make the change. That's not so difficult with search and replace, but it's not just a single point of change. Also, you might be tempted to start interacting with the implementation class directly and forget about updating the abstract class. And if that happened it would be much more difficult to change implementations. You might as well not even use an abstract class in that case.

2. You're creating a new instance every time get your service with `DataBaseFirebaseService()`. Maybe for Firebase that wouldn't matter. I haven't done much with Firebase so I don't know. If it were a local SQL database, though, you'd get issues with having more than one connection to the database open. For a lot of services you want to guarantee only one instance in the whole app, ie, a singleton. Now you could just create a regular singleton and in that way you could get rid of GetIt. I hear there are problems with testing singletons, though. I haven't actually tried myself, though. Another option would be a global variable that you initialize once when the app starts. I know global variables are generally frowned on but maybe it would work for a service. Again, I haven't done much research in this area.

--

--

Suragch
Suragch

Written by Suragch

Flutter and Dart developer. Twitter: @suragch1, Email: suragch@suragch.dev

No responses yet