Blue
Blue

Reputation: 465

Class does not implement abstract base class member public abstract fun create(p0: Context?, p1: Int, p2: Any?): PlatformView

I have this compile error after following the guide in https://docs.flutter.dev/development/platform-integration/platform-views

I have tried using Flutter v3.0.1 and v2.13.0-0.4.pre to test but still the same error.

Error is shown that:

e: C:\Users\wongc\Documents\Agmo_Studio_Project\ble_poc\android\app\src\main\kotlin\com\example\ble_poc\MapViewFactory.kt: (9, 1): Class 'MapViewFactory' is not abstract and does not implement abstract base class member public abstract fun create(p0: Context?, p1: Int, p2: Any?): PlatformView defined in io.flutter.plugin.platform.PlatformViewFactory e: C:\Users\wongc\Documents\Agmo_Studio_Project\ble_poc\android\app\src\main\kotlin\com\example\ble_poc\MapViewFactory.kt: (10, 5): 'create' overrides nothing

MainActivity.kt

package com.example.ble_poc

import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine

class MainActivity : FlutterActivity() {
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        flutterEngine
                .platformViewsController
                .registry
                .registerViewFactory("mappedin", MapViewFactory())
    }
}

MapView.kt

package com.example.ble_poc

import android.content.Context
import android.graphics.Color
import android.view.View
import android.widget.TextView
import io.flutter.plugin.platform.PlatformView

internal class MapView(context: Context, id: Int, creationParams: Map<String?, Any?>?) : PlatformView {
    private val textView: TextView

    override fun getView(): View {
        return textView
    }

    override fun dispose() {}

    init {
        textView = TextView(context)
        textView.textSize = 72f
        textView.setBackgroundColor(Color.rgb(255, 255, 255))
        textView.text = "Rendered on a native Android view (id: $id)"
    }
}

MapViewFactory

package com.example.ble_poc

import android.content.Context
import android.view.View
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory

class MapViewFactory : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
    override fun create(context: Context, viewId: Int, args: Any?): PlatformView {
        val creationParams = args as Map<String?, Any?>?
        return MapView(context, viewId, creationParams)
    }
}

Is this an error on Flutter side?

Upvotes: 3

Views: 4745

Answers (1)

baka3k
baka3k

Reputation: 186

It's error from null safety of kotlin

please try below

class MapViewFactory  : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
    override fun create(context: Context?, viewId: Int, args: Any?): PlatformView {
        val creationParams = args as Map<String?, Any?>?
        return MapView(context!!, viewId, creationParams)
    }
}

change source code from

    override fun create(context: Context, viewId: Int, args: Any?):

to

    override fun create(context: Context?, viewId: Int, args: Any?): 
    /// you are missing '?' from 'context:Context'

From detail, you can see PlatformViewFactory source code:

 public abstract PlatformView create(@Nullable Context context, int viewId, @Nullable Object args);

context is nullable - so your are missing '?' in kotlin null safety

Upvotes: 6

Related Questions