Firebase + Flutter Integration (Android)
Firebase is an app development platform developed by Google for building, creating and growing mobile and web applications. With Firebase, developers need not focus so much on the backend — servers, APIs, storage, analytics etc — as the platform takes care of all these needs.
Once you have created your Flutter application, integrating Firebase to a Flutter application can be done in a few simple steps.
Go to the Firebase Console and sign in with your Google account. Click add project to create a new project.
Enter your project name and click continue.
The next step recommends you to add Google Analytics to the project. You can opt in and select or create a Google Analytics account in the 3rd step. For this demo, I opted out and proceeded to create the project. It will take a couple of seconds to set up the project.
Once the project is ready, click on continue.
The next step will be to link the Firebase project we’ve just created to the Flutter project.
Click on the Android icon.
1. To register the Android application, you’ll need to add the package name (required) and the app nickname (optional). You can get the package name from android > app > build.gradle file
defaultConfig {
applicationId "com.flutter.flutter_firebase_app"
}
When you have entered the required package name. Register the app.
2. Download the google-services.json file and copy it in the Android > app directory. This json file contains all then information needed to like the Firebase project to the Flutter project. Ensure the file name is not modified in anyway — if you have downloaded the file before, it might be appended with additional numbers to differentiate them. Rename it to the default google-services.json before you add it to the Flutter project. Click next when done.
3. We’ll need to modify the gradle file to use the plug-in. Copy the classpath and add it to the project-level build.gradle (<project>/build.gradle
):
classpath ‘com.google.gms:google-services:4.3.13’
Then add the Google services Gradle plugin to the app-level build.gradle.
At the bottom of the file (android/app/build.gradle
):
apply plugin: 'com.google.gms.google-services'
4. Click next and continue to console
In the pubspec.yaml file, add some Firebase dependencies e..g. cloud firestore. Run flutter pub get to fetch the dependencies.
cloud_firestore: ^3.4.4
Lastly, add Firebase to the main method of the project to initialise it when the application runs.
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
Note: The cloud_firestore plugin requires a higher Android SDK version. Thus, to avoid issues while compiling set the minSdkVersion to at least 19. You can use higher versions as well e.g. 21
However, this means that the app won’t be available to users running android SDKs below what you have set as the minimum. Alternatively, try to find a version of this plugin that supports these lower Android SDK versions.
If there are no issues with the Firebase configuration, your app should load successfully after these steps.
There you have it. Firebase is now integrated with your Flutter app and you can start making use of the available features.
As always, thank you for reading ❤