Class 没有实现抽象基础 class 成员 public abstract fun create(p0: Context?, p1: Int, p2: Any?): PlatformView

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

我按照 https://docs.flutter.dev/development/platform-integration/platform-views

中的指南进行编译后出现此错误

我试过使用Flutter v3.0.1和v2.13.0-04.pre进行测试,还是一样的错误

错误显示:

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)
    }
}

这是Flutter端的错误吗?

kotlin 的空安全性错误

请尝试以下

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)
    }
}

更改源代码
    override fun create(context: Context, viewId: Int, args: Any?):

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

详细可以看PlatformViewFactory源码:

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

context 可以为 null - 所以你缺少'?'在科特林空安全