Skip to content

Install the Firebase Backend

For Firebase to work with your Flutter app, configuration of both Firebase and your Flutter app has to be done. Please follow the step below in details, since the level of debugging / error messages are quite limited when setting this up. If you are new to Firebase, then please start by reading the extensive Firebase documentation first. Then read the Add Firebase to your Flutter app tutorial.

  1. Create a Firebase project with a cloud storage
  2. Add Firebase to your Flutter project by following the description. Note that you need to add support for both the Android and iOS version of the Flutter app.
  3. Configure authentication
  4. Add users that can upload data e.g. via the console
  5. Set up your storage security rules as shown below.
# Default access rule
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}

For testing/debugging purposes, you may remove authentication by the authentication rule below (but should be removed in production)

# No authentication rule
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}

Step 2: Configure your app to use Firebase

Section titled “Step 2: Configure your app to use Firebase”

Follow the direction on how to configure a Flutter app to use Firebase.

  1. Add your app to your Firebase project. Enter your app’s bundle ID in the iOS bundle ID field.
  2. Download the GoogleService-Info.plist file to obtain your Firebase iOS config file.
  3. Using XCode, move the file into the Runner/Runner directory of your Flutter app.
  4. Google services use CocoaPods to install and manage dependencies. Open a terminal window and navigate to the location of the Xcode project for your app. The type
$ pod init
§ pod install

This should install all the necessary pods for Firebase.

Note: It seems like Firebase is putting constraints on the naming of the iOS Bundle Name. Even though a valid iOS bundle name can contain . (dots) and spaces, Firebase throws the following exception:

Terminating app due to uncaught exception 'com.firebase.core', reason: 'App name can only contain alphanumeric, hyphen (-), and underscore (_) characters'

if dots are used. Hence, avoid using dots, but use - (hypen) instead. This is specified in the `ios/Runner/info.plist’ file:

<key>CFBundleName</key>
<string>your-app-name</string>

Note – it seems like adding Firebase support to your iOS app dramatically increases the Xcode build process for the Flutter app. So – have patience…

  1. Add your app to your Firebase project. Enter your app’s application ID in the Android package name field.

  2. Download the configuration file named google-services.json and make sure to put in in your Android app module root directory, i.e a folder like <appname>/android/app/

  3. Add support for the Google Services Gradle plugin to read the google-services.json file that was generated by Firebase.

    • in your IDE, open android/app/build.gradle, and add the following line as the last line in the file:

    apply plugin: 'com.google.gms.google-services'

    • In android/build.gradle, inside the buildscript tag, add a new dependency:
    dependencies {
    // ...
    classpath 'com.google.gms:google-services:4.3.2' // new
    }

Step 3: Use the CARP Firebase Backend in your Flutter App

Section titled “Step 3: Use the CARP Firebase Backend in your Flutter App”

In Flutter, the carp_firebase_backend plugin is used to upload data from carp_mobile_sensing to a Firebase endpoint.

Add the carp_firebase_backend plugin to the pubspec.yaml file.

dependencies:
flutter:
sdk: flutter
carp_firebase_backend: ^0.5.0 # support for uploading CARP data to Firebase

Run flutter packages get. For more information on managing packages and plugins, see Using Packages.