I am trying to create a plugin for Flutter in Java. I am trying to pass the Android activity
parameter in my main Java
class.
This is my native code:
package com.example.trsurveys;
import androidx.annotation.NonNull;
import android.app.Activity;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import theoremreach.com.theoremreach.TheoremReach;
public class TrsurveysPlugin implements FlutterPlugin, MethodCallHandler {
private MethodChannel channel;
private Registrar registrar;
private Activity activity;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "trsurveys");
channel.setMethodCallHandler(this);
}
When I try to debug my app it shows me the following errors
private TrsurveysPlugin(Activity activity) {
this.activity = activity;
}
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "trsurveys");
channel.setMethodCallHandler(new TrsurveysPlugin(registrar.activity()));
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("init")) {
String apiKey = call.argument("apiKey");
TheoremReach.initWithApiKeyAndUserIdAndActivityContext(apiKey, "USER_ID", activity);
}
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
}
When I try to debug my app I get the following errors:
error: constructor TrsurveysPlugin in class TrsurveysPlugin cannot be applied to given types;
flutterEngine.getPlugins().add(new com.example.trsurveys.TrsurveysPlugin());
^
required: Activity
found: no arguments
reason: actual and formal argument lists differ in length
...path/Downloads/TheoremReach-error/example/android/app/src/main/java/com/example/trsurveys_example/MainActivity.java:11: error: incompatible types: MainActivity cannot be converted to FlutterEngine
GeneratedPluginRegistrant.registerWith(this);
^
This is the code that appears in my GeneratedPluginRegistrant.java
file.
package io.flutter.plugins;
import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.FlutterEngine;
@Keep
public final class GeneratedPluginRegistrant {
public static void registerWith(@NonNull FlutterEngine flutterEngine) {
flutterEngine.getPlugins().add(new com.example.trsurveys.TrsurveysPlugin());
}
}
While making the Plugin I have tried deleting the GeneratedPluginRegistrant.java file but every time it gets generated with the same configurations.
Open your project manifest file and remove this Lines
meta-data
android:name=”flutterEmbedding”
android:value=”2″
Then build and run it again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With