Native view inside flutter app error - cannot register plugin

Viewed 375

what I was trying to achieve is to embed a native view inside a flutter app. Something similar to what's explained here.

My problem is that i get the following error:

Unhandled Exception: PlatformException(error, java.lang.IllegalStateException: Trying to create a platform view of unregistered type: com.example.cat_app/textview

So I looked up how to register plugins. I tried the following code but cannot get it to work. Doing some research it seems that from flutter 1.12+ plugins register automatically so it's not needed to register them. But my code stills give the unregistered plugin error...

The following code gives me unresolved reference registrarFor error btw

class MainActivity: FlutterActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    GeneratedPluginRegistrant.registerWith(this)
    WebViewPlugin.registerWith(this.registrarFor("com.yourPackageName"))
  }
}

I'm stuck here and I don't know what should I do to register the plugin in the recent versions of Flutter. Is there any way I can achieve what I need? Thanks in advance

1 Answers

To register the custom plugin try the approach mentioned https://flutter.dev/docs/development/platform-integration/platform-views

  1. For the MainActivity since it implements the io.flutter.embedding.android.FlutterActivity class, you will need to override the configureFlutterEngine(...) method

  2. Include the following WebViewPlugin.registerWithV2(flutterEngine);

  3. Pass the flutter engine and register as follows:

    public static void registerWithV2(@NonNull FlutterEngine flutterEngine) {
    
       BinaryMessenger _messenger = flutterEngine.getDartExecutor().getBinaryMessenger();
    
       flutterEngine.getPlatformViewsController().getRegistry().registerViewFactory("MyViewTypeId", new CWebFactory(_messenger));
    }
    
Related