![](https://flutterdevelop.blog/wp-content/uploads/2024/08/Easy-Guide-Connecting-Your-Flutter-App-to-Firebase-1024x538.png)
In this post, I will guide you through the steps to use Firebase in your Flutter app. Primarily, we will follow the official instructions provided below.
<Official> Add Firebase to your Flutter app
However, I encountered several issues during the process, so I will also share the solutions to these problems.
Environment
Here is my development environment:
- Windows 11
- VSCode
- Flutter 3.22.3
- Google Pixel [Android] (Debug device)
Requirements
You will need the following to proceed with this setup:
- The Flutter app project to connect
- A Google account to manage the Firebase project
- The prerequisites listed in the official documentation
<Official> Add Firebase to your Flutter app
You do not need to prepare a new Firebase project at this stage. If you already have a project, you can use it.
Installing Firebase CLI
Firebase CLI is a Command Line Interface to connect to Firebase. You need Firebase CLI to access Firebase. Follow the official instructions to install it.
<Official> Firebase CLI Reference
I wanted to run it in the VSCode terminal, so I installed it via npm. Here is the npm installation method for your reference.
Installing Node.js
If you already have Node.js installed, you can skip this step. Download the Node.js installer from the link below and install Node.js.
https://nodejs.org/en
Installing Firebase CLI
Run the following command in your terminal.
npm install -g firebase-tools
With this, Firebase CLI is ready.
Logging into Firebase
Run the following command in the terminal to log into Firebase.
firebase login
A Google account screen will open, select the account you want to use.
![](https://flutterdevelop.blog/wp-content/uploads/2024/07/image-3.png)
Installing FlutterFire CLI
FlutterFire CLI is a set of Flutter plugins to connect your Flutter app to Firebase. Run the following command in the terminal to log into Firebase.
dart pub global activate flutterfire_cli
Configuring Firebase in Your Flutter App
Run the following command in the terminal to configure the Firebase connection.
flutterfire configure
Error: flutterfire: command not found
If you encounter the error “flutterfire: command not found,” it means that the ‘flutterfire’ command is not recognized as an internal or external command. You can resolve this by setting the system environment variables.
Add the following to the Path in your system environment variables.
C:\Users\{UserName}\AppData\Local\Pub\Cache\bin
Error: FirebaseCommandException
If the above step is completed, you might encounter a new error “FirebaseCommandException: An error occurred on the Firebase CLI when attempting to run COMMAND: firebase projects:list –json”. This error is due to an unsuccessful login.
Run the following command in the terminal to log in again.
firebase login --reauth
Selecting a Project
When you run “flutterfire configure”, you will be asked to select a project. Choose an existing project or select “create a new project” to create a new one.
![](https://flutterdevelop.blog/wp-content/uploads/2024/07/image-5.png)
After selecting the project and platform, the Firebase connection settings will be applied to your Flutter app project.
Installing firebase_core
Add firebase_core to your pubspec.yaml (version is optional).
dependencies:
firebase_core: ^3.2.0
Setting Up main.dart
Add the initial processing to connect to Firebase in main.dart.
Future<void> main() async {
// firebase
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
Encountering an Error…
After setting up, when I ran it in debug mode, I encountered the following error. It appears that the classpath for “com.google.gms:google-services” is missing.
Could not find method classpath() for arguments [com.google.gms:google-services:4.3.15]
on root project 'android' of type org.gradle.api.Project.
Solution
Fix the following three files:
- android\build.gradle
- android\settings.gradle
- android\app\build.gradle
android\build.gradle
Delete the “buildscript” section.
- buildscript {
- ext.kotlin_version = '1.7.10'
- repositories {
- google()
- mavenCentral()
- }
- dependencies {
- // START: FlutterFire Configuration
- classpath 'com.google.gms:google-services:4.3.15'
- // END: FlutterFire Configuration
- classpath 'com.android.tools.build:gradle:7.3.0'
- classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
- }
- }
allprojects {
repositories {
google()
mavenCentral()
}
}
android\settings.gradle
Add “com.google.gms.google-services” to “plugins” as follows (version is optional).
plugins {
id "dev.flutter.flutter-plugin-loader" version '1.0.0'
id "com.android.application" version '7.3.0' apply false
id "org.jetbrains.kotlin.android" version '1.7.10' apply false
+ id "com.google.gms.google-services" version '4.3.15' apply false
}
android\app\build.gradle
Remove the automatically added “apply plugin: ‘com.google.gms.google-services'” line and add “id ‘com.google.gms.google-services'” as shown below.
plugins {
id "com.android.application"
- // START: FlutterFire Configuration\
- apply plugin: 'com.google.gms.google-services'
+ id "com.google.gms.google-services"
- // END: FlutterFire Configuration
id "org.jetbrains.kotlin.android"
id "dev.flutter.flutter-gradle-plugin"
}
Still Encountering Errors…
When I ran the debug mode again, the following message appeared. It seems the minSdkVersion is incorrect.
│ android { │
│ defaultConfig { │
│ minSdkVersion 23 │
│ } │
│ } │
│ │
│ Following this change, your app will not be available to users running Android SDKs below 23. │
│ Consider searching for a version of this plugin that supports these lower versions of the │
│ Android SDK instead. │
│ For more information, see: │
│ https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration │
└───────────────────────────────────────────────────────────────────────────────────────────────┘
Error: Gradle task assembleDebug failed with exit code 1
Solution 1
Modify the following line in android\build.gradle.
defaultConfig {
- minSdkVersion flutter.minSdkVersion
+ minSdkVersion 23
}
Solution 2
Alternatively, change the default value of minSdkVersion in the Flutter JDK file located at “…\flutter\packages\flutter_tools\gradle\src\main\groovy\flutter.groovy”.
class FlutterExtension {
/** Sets the compileSdkVersion used by default in Flutter app projects. */
public final int compileSdkVersion = 34
/** Sets the minSdkVersion used by default in Flutter app projects. */
- public final int minSdkVersion = 19
+ public final int minSdkVersion = 23
I chose this method because future apps might also need to access Firebase.
Confirming Debugging Works!
After running the debug mode again, it executed successfully!
Launching lib\main.dart on Pixel X in debug mode...
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8
√ Built build\app\outputs\flutter
-apk\app-debug.apk
Connecting to VM Service at ws://127.0.0.x:xxxx/vv-oYScA6OI=/ws