Flutter, Firestore and Dependency Injection

In this article, I will share my ideas about how the coding process of integrating Flutter with Firestore can be improved using dependency injection for building production-ready apps.

Hello everyone! Today, I wanted to share with you all an amazing article that I found on the Flutter-Dev community’s LinkedIn page:

https://medium.com/flutter-community/flutter-firestore-you-may-be-using-it-wrong-b56fa689e489

In this article, they talk about how easy it is to integrate Flutter with Firebase, and how this can help you in order to build your app faster, (which I definitely agree with), but sometimes not in the best way possible to be deployed or launched to production. This happens because, due to this speediness and fast development, some problems may happen in the future if we want to maintain our code or scale and improve our already-deployed app.

 

In order to fix this problem, they give us two ways to improve our app: to continuously develop it with testability and maintainability. They suggest we use Dependency Injection, using the “Provider” pattern to be able to externalize Firebase’s Repositories, injecting them into our widgets and just calling their operations in order to read or write the data from or into them.

 

Service Locator

Personally, I want to add one additional comment or suggestion: for this purpose, you can use too the class “Service Locator” of Flutter, which allows you to use Dependency Injection in your app. With it, you can create your services and externalize the logic inside them, and then inject that dependency with this locator on your widgets, separating the User Interface with the Business Logic, something that will definitely help you make your app a more scalable one, and your code a more readable one too.

 

This is the Dart package‘s link to add Service Locator to your pubspeck.yaml: https://pub.dev/packages/get_it

 

You can still continue using the Provider’s class for your Firebase Repositories if you want to, but personally, I found this solution pretty understandable and straight-forward, and I suggest you to, at least, take a look into it to see how easy it is to apply it in your code.

So to summarize, Dependency Injection in a Flutter + Firebase app will definitely make your life easier!

 

Thank you very much for taking the time to read this post! I hope you found this article helpful and I really encourage you to start a Flutter app if you haven’t done it yet, you will learn so much along the way ūüėÄ ¬†

M√°s Posts