Adding Cloud Firestore & Firebase to Flutter project (Android)

2,724

Solution 1

As I discussed this with a few co-developers, I discovered that one had found a working setup: changing the version of cloud_firestore to be below 0.3.0, 0.2.9 for instance. According to this recently published issue, the issue is that the 'stable' version of flutter doesn't have certain features that cloud_firestore uses, which causes the errors you see when compiling. I'll update the question in a while to reflect this answer if requested.

Solution 2

I solved my current Cloud_Firestore problems by:

Hope this would help. I spent hours trying to figure it out why I could not compile my flutter app with cloud_firestore. I knew that the issue was around the flutter SDK versions, but I did not wanted to go for alpha. But the solution turned out to be as simple as using the master channel. Although, not sure yet what other issues may arise in the near future. However now, I can happily start testing with cloud firestore. ;)

Share:
2,724
Pluriscient
Author by

Pluriscient

Updated on December 04, 2022

Comments

  • Pluriscient
    Pluriscient over 1 year

    This is going to be a fairly long question, so I'll include a TL;DR at the end.

    For days now, I've been trying to find a way to make my setup work, but to no avail. Now to make it complete and of aid to others, I'll be recording my steps here:

    Steps to reproduce

    1. Create a new Flutter project (compiles and runs without any warnings)
    2. Create a new Firebase console project
    3. Follow these instructions to add Firebase to android (also included below in case of version changes).
    4. Add Cloud Firestore to the project in the pubspec.yaml
    5. Encounter this error:

      • What went wrong: Execution failed for task ':cloud_firestore:compileDebugJavaWithJavac'.

    Firebase instructions (as of now)

    1. Install Google Repository (revision 58 currently) (not necessary anymore according to this issue, but done for verification).
    2. Copy the package name found in android/app/src/main/AndroidManifest.xml. In my case this was com.omnisciamus.fire. Use it to create a new android app in the Firebase project along with a debug key.
    3. Drop the created google-services.json in android/app
    4. Modify the android/build.gradle and android/app/build.gradle. I've changed this to using the latest versions (modifications included below).
    5. At this point I reran the compilation to see if any issues popped up already, and we have 3 of these warnings, which is most likely the google services plugin, not something I can change anything about: registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection)
    6. Now we add the dependencies in the pubspec.yaml, also included below. Now there are several possible versions we can use, but I first tried the latest versions (by including a ^ in front of the versions).

    Gradles

    Changes to the android/build.gradle:

    buildscript {
            repositories {
                google()
                jcenter()
            }
    
            dependencies {
                classpath 'com.android.tools.build:gradle:3.1.0' // upgraded from 3.0.1
                classpath 'com.google.gms:google-services:3.2.0' // added & upgraded from 3.1.1
            }
        }
    

    And then we have the android/app/build.gradle:

    //bottom of file
    apply plugin: 'com.google.gms.google-services'
    

    Pubspec

    The dependencies structure of my pubspec looks like this:

    dependencies:
      flutter:
        sdk: flutter
      cupertino_icons: ^0.1.0
      firebase_analytics: 0.3.0
      cloud_firestore: 0.3.1
    

    Setup details

    For additional details I've included my flutter doctor -v output:

    [√] Flutter (Channel beta, v0.1.5, on Microsoft Windows [Version 10.0.16299.309], locale en-GB)
        • Flutter version 0.1.5 at C:\Developer\Flutter\flutter
        • Framework revision 3ea4d06340 (5 weeks ago), 2018-02-22 11:12:39 -0800
        • Engine revision ead227f118
        • Dart version 2.0.0-dev.28.0.flutter-0b4f01f759
    
    [√] Android toolchain - develop for Android devices (Android SDK 27.0.0)
        • Android SDK at C:\Android
        • Android NDK at C:\Android\ndk-bundle
        • Platform android-P, build-tools 27.0.0
        • ANDROID_HOME = C:\Android
        • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
        • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-915-b01)
    
    [√] Android Studio (version 3.0)
        • Android Studio at C:\Program Files\Android\Android Studio
        • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-915-b01)
    
    [√] IntelliJ IDEA Ultimate Edition (version 2017.3)
        • Flutter plugin version 22.2.2
        • Dart plugin version 173.4548.30
    
    [√] VS Code (version 1.21.1)
        • VS Code at C:\Program Files\Microsoft VS Code
        • Dart Code extension version 2.10.0
    
    [√] Connected devices (1 available)
        • Android SDK built for x86 • emulator-5554 • android-x86 • Android 6.0 (API 23) (emulator)
    
    • No issues found!
    

    Debug key

    As this may become a tutorial I'll include a simple command to obtain the debug key (on Windows): cd %JAVA_HOME%\bin & keytool -exportcert -list -v -alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore

    TL;DR

    Cloud_firestore crashes with a version of 0.3.x, how do you fix this?