Solvedflutterfire [firebase_messaging] Type mismatch: inferred type is PluginRegistry? but FlutterEngine was expected

Following the instructions on pub.dev for handling background messages doesn't compile for Android since the GeneratedPluginRegistrant.registerWith method takes a FlutterEngine object as a parameter. Using the PluginRegistry provided by the method throws the error

Type mismatch: inferred type is PluginRegistry? but FlutterEngine was expected

Please see the below code for reference

package ...

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

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

    override fun registerWith(registry: PluginRegistry?) {
        GeneratedPluginRegistrant.registerWith(registry) //Error thrown here
    }
}

To reproduce
Steps to reproduce the behavior:

  1. Follow the steps in the given link up to step 1 of Optionally handle background messages
  2. Build the project

Expected behavior
Somehow a FlutterEngine object should be passed to the method in order to successfully build for Android

12 Answers

βœ”οΈAccepted Answer

Fixed by replace with:

class MainApplication : FlutterApplication(), PluginRegistrantCallback {

    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    override fun registerWith(registry: PluginRegistry?) {
        registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin");
    }

}

Other Answers:

This does allow me to compile, but I'm not sure if the background service is being registered correctly since I get the error

E/flutter (12123): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method FcmDartService#initialized on channel plugins.flutter.io/firebase_messaging_background)
E/flutter (12123): #0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:319:7)
E/flutter (12123): <asynchronous suspension>
E/flutter (12123): #1      _fcmSetupBackgroundChannel (package:firebase_messaging/firebase_messaging.dart:51:21)
E/flutter (12123): #2      _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:45:6)
E/flutter (12123): #3      _fcmSetupBackgroundChannel (package:firebase_messaging/firebase_messaging.dart:24:32)
E/flutter (12123): #4      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:239:25)
E/flutter (12123): #5      _rootRun (dart:async/zone.dart:1126:13)
E/flutter (12123): #6      _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (12123): #7      _runZoned (dart:async/zone.dart:1518:10)
E/flutter (12123): #8      runZoned (dart:async/zone.dart:1502:12)
E/flutter (12123): #9      _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:231:5)
E/flutter (12123): #10     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:307:19)
E/flutter (12123): #11     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)

Doesn't the replacement simply return a Registrar object without registering anything?

Try this replace @Jacobsjj2

    override fun registerWith(registry: PluginRegistry?) {
  io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }

Fixed by replace with:

class MainApplication : FlutterApplication(), PluginRegistrantCallback {

    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    override fun registerWith(registry: PluginRegistry?) {
        registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin");
    }

}

tq work for me

This does allow me to compile, but I'm not sure if the background service is being registered correctly since I get the error

E/flutter (12123): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method FcmDartService#initialized on channel plugins.flutter.io/firebase_messaging_background)
E/flutter (12123): #0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:319:7)
E/flutter (12123): <asynchronous suspension>
E/flutter (12123): #1      _fcmSetupBackgroundChannel (package:firebase_messaging/firebase_messaging.dart:51:21)
E/flutter (12123): #2      _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:45:6)
E/flutter (12123): #3      _fcmSetupBackgroundChannel (package:firebase_messaging/firebase_messaging.dart:24:32)
E/flutter (12123): #4      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:239:25)
E/flutter (12123): #5      _rootRun (dart:async/zone.dart:1126:13)
E/flutter (12123): #6      _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (12123): #7      _runZoned (dart:async/zone.dart:1518:10)
E/flutter (12123): #8      runZoned (dart:async/zone.dart:1502:12)
E/flutter (12123): #9      _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:231:5)
E/flutter (12123): #10     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:307:19)
E/flutter (12123): #11     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)

Doesn't the replacement simply return a Registrar object without registering anything?

Sorry for the delay! @hsangtini's latest suggestion works. I guess it would also be necessary to update the documentation to that then?

More Issues: