jonasbark / flutter_in_app_update

Flutter Plugin: Enables In App Updates on Android using the official Android APIs.
Other
323 stars 79 forks source link

registrar.activity() must not be null #10

Closed creativecreatorormaybenot closed 4 years ago

creativecreatorormaybenot commented 4 years ago

I am not quite sure why this is happening - I assume because you are using Kotlin and the value is not nullable.

Problem

Adding a custom application, which is needed for some stuff like android_alarm_manager, like this:

public class Application extends FlutterApplication implements PluginRegistrantCallback {
  @Override
  public void onCreate() {
    super.onCreate();
    FlutterFirebaseMessagingService.setPluginRegistrant(this);
  }

  @Override
  public void registerWith(PluginRegistry registry) {
    GeneratedPluginRegistrant.registerWith(registry);
  }
}

When adding this and setting android:name=".Application" in the application tag in the manifest, InAppUpdatePlugin will fail when FlutterFirebaseMessagingService calls this. The error is the following:

E/MethodChannel#plugins.flutter.io/firebase_messaging(12112): Failed to handle method call
E/MethodChannel#plugins.flutter.io/firebase_messaging(12112): java.lang.IllegalStateException: registrar.activity() must not be null
E/MethodChannel#plugins.flutter.io/firebase_messaging(12112):   at de.ffuf.in_app_update.InAppUpdatePlugin$Companion.registerWith(InAppUpdatePlugin.kt:27)
E/MethodChannel#plugins.flutter.io/firebase_messaging(12112):   at de.ffuf.in_app_update.InAppUpdatePlugin.registerWith(Unknown Source:2)
E/MethodChannel#plugins.flutter.io/firebase_messaging(12112):   at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(GeneratedPluginRegistrant.java:44)
E/MethodChannel#plugins.flutter.io/firebase_messaging(12112):   at Application.registerWith(Application.java:18)

Reason

The reason I concluded that this plugin is faulty here is that I have 20 other plugins in the project and commenting out the following completely resolves the error:

//            val instance = InAppUpdatePlugin(registrar.activity())
//            registrar.addActivityResultListener(instance)
//            registrar.activity().application.registerActivityLifecycleCallbacks(instance)
//            channel.setMethodCallHandler(instance)

I could not figure out more about this, but I think that the plugin should be able to handle the scenario.

jonasbark commented 4 years ago

Thanks for letting me know! Can you try the branch fixes/10?

creativecreatorormaybenot commented 4 years ago

@jonasbark Works, thanks a lot!

jonasbark commented 4 years ago

Uploaded.