Flutter 1.12 to 1.22 update and issues with firebase libraries

Upgrading from my old Flutter v1.12.13+hotfix.5 up to Flutter 1.22.0 stable went mostly like a charm. You should go through the 1.12 migration guide. The problems came after upgrading Firebase dependencies.

firebase_core: ^0.5.0
firebase_auth: ^0.18.1+1
firebase_messaging: ^7.0.2

The very first assembling failed with:

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/idezhin/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_auth-0.18.1+1/android/build.gradle' line: 54

* What went wrong:
A problem occurred evaluating project ':firebase_auth'.
> Could not find method platform() for arguments [com.google.firebase:firebase-bom:25.3.1] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Update distributionUrl in android/gradle/wrapper/gradle-wrapper.properties update.
My old value was https://services.gradle.org/distributions/gradle-4.10.2-all.zip

...
distributionUrl=https://services.gradle.org/distributions/gradle-5.4.1-all.zip


After the assembling was stabilised the error occurred during app start-up

E/flutter (24642): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
E/flutter (24642): #0      MethodChannelFirebase.app (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:118:5)
E/flutter (24642): #1      Firebase.app (package:firebase_core/src/firebase.dart:52:41)
E/flutter (24642): #2      FirebaseAuth.instance (package:firebase_auth/src/firebase_auth.dart:37:47)

“No Firebase App ‘[DEFAULT]’ has been created” is indicating that starting from firebase_core: ^0.5.0 due to internal architecture changes all firebase calls should be preceded with initialization `Firebase.initializeApp()`.
Here is a detailed explanation at StackOverflow.


After initializing firebase properly there was another one:

E/flutter (27772): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: PlatformException(error, PluginRegistrantCallback is not set., null, java.lang.RuntimeException: PluginRegistrantCallback is not set.
E/flutter (27772): 	at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.java:157)
E/flutter (27772): 	at io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.onMethodCall(FirebaseMessagingPlugin.java:174)
E/flutter (27772): 	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/flutter (27772): 	at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)

To fix it change (or add) your android/app/src/main/kotlin/%your_package%/Application.kt class.
And add FirebaseCloudMessagingPluginRegistrant.kt.
Many thanks to marwfair who figured it out in an issue thread.

package your_package;

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this)
    }

    override fun registerWith(registry: PluginRegistry) {
        FirebaseCloudMessagingPluginRegistrant.registerWith(registry)
    }
}
package your_package;

import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin

object FirebaseCloudMessagingPluginRegistrant {
    fun registerWith(registry: PluginRegistry?) {
        if (alreadyRegisteredWith(registry)) {
            return
        }
        FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
    }

    private fun alreadyRegisteredWith(registry: PluginRegistry?): Boolean {
        val key: String? = FirebaseCloudMessagingPluginRegistrant::class.java.canonicalName
        if (registry?.hasPlugin(key)!!) {
            return true
        }
        registry.registrarFor(key)
        return false
    }
}


If you’re adding Application.kt file, don’t forget to link it in AndroidManifest.xml cause it is not necessary to have it in Flutter 1.22.

<application
        android:name=".Application"
        ...
>
...
</application>

Leave a Reply

Your email address will not be published. Required fields are marked *