Setting up your Flutter app with Firebase
Tags: firebase, flutter, appdevelopment, google
Published: 2020-05-29 16:17:42

Lets create a Flutter app and get it all setup with Firebase.

# Create your Flutter App

Create your app and open it in your IDE of choice.
```bash
flutter create my_app
```

## Install Dependencies
Below are the dependneices for Firebase in Flutter, assuming you're using Firestore and Google Signin. 
```yaml
dependencies:
  flutter:
    sdk: flutter

  firebase_core: ^0.4.0
  firebase_analytics: ^1.0.4

  cloud_firestore: ^0.8.2+3

  firebase_auth:  ^0.6.6
  google_sign_in: ^3.2.4
```

# Setup Firebase

## Android Setup

### Generate your SHA Certificates
Make sure you have [Java](https://www.java.com/en/) installed. On Ubuntu, you can install it by running `sudo apt-get install default-jdk`.

Open up your terminal at the Flutter project root, and run `cd android` to mvoe into the android directory. Then run `./gradlew signingReport` to generate the SHA1 and SHA256 keys. Copy and paste them from the `Varient: debug` section

Make sure you know your application ID, this can be found in the `android/app/build.gradle` file under your `defaultConfig`. Grab that for the next step.

### Create your Firebase Project

![Create your Firebase Project](https://i.imgur.com/3rpJcUj.png)

Go to the [Firebase console](console.firebase.google.com) and create your new project. Register your app by clicking ***Add Firebase to your  Android app.*** Enter your project ID,  package name,. and the SHA-1 Certificate from the last step. Download the `google-services.json` file to the `android/app` directory. 

![Android Setup Page](https://i.imgur.com/pyAGpak.png)

Now click through the rest of the setup, and go into your project settings. In the general tab, you'll see a section called "Your apps". Click the "Add fingerprint" section and copy and paste the SHA256 key from the last step, then redownload the config file. 

### Update the build.gradle files
Lets register the Google Services in the Gradle build files for Android. 

Lets start with the one in `android/build.gradle`

```gradle

buildscript {
  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
  }
  dependencies {
    ...
    // Add this line
    classpath 'com.google.gms:google-services:4.3.2'
  }
}

allprojects {
  ...
  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
    ...
  }
}

```

And then the `android/app/build.gradle`

```gradle
 apply plugin: 'com.android.application'

dependencies {
  // add the Firebase SDK for Google Analytics
  implementation 'com.google.firebase:firebase-analytics:17.2.0'
  // add SDKs for any other desired Firebase products
  // https://firebase.google.com/docs/android/setup#available-libraries
}
...
// Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'
```

That's it! Try running it with an Android emulator or device to verify everything worked.

## iOS Setup

The iOS setup is less tedious and can be completed in one step.

![IOS setup](https://i.imgur.com/ieoJR5U.png)

### Register and Download the GoogleService-Info.plist

Click  _add your app to iOS_  then download the  `GoogleService-Info.plist`  file into the  `ios/Runner/Runner`  directory **from XCode**. Make sure that your application ID matches in registration and in XCode.