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

标签 android flutter kotlin

按照 https://docs.flutter.dev/development/platform-integration/platform-views 中的指南进行操作后出现此编译错误

我试过用Flutter v3.0.1和v2.13.0-0.4.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)"
    }
}

map View 工厂

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 null safety的错误

请尝试以下

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 - 所以你缺少'?'在 kotlin 空安全中

关于android - 类没有实现抽象基类成员 public abstract fun create(p0 : Context? , p1 : Int, p2 : Any? ): PlatformView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72357394/

相关文章:

guava - 在 Kotlin 中,如何委托(delegate)给一个接口(interface)并只提供一个无参数的公共(public)构造函数?

android - 无法解决 : com. google.firebase :firebase-core:11. 2.0

android - 在Android Gradle build.gradle it.buildConfigField中的 "it"是什么?

flutter - 插件项目 :path_provider_macos not found. 请更新 settings.gradle

flutter - 解密即将到来的文本 flutter

kotlin - 可空和不可空参数的相同方法

android - 找不到参数的方法 classpath() [com.android.tools.build :gradle:3. 4.2]

Android 背景 - 拉伸(stretch) - 9Patch 问题

Flutter 圆形头像带边框

java - 没有Maven Central的Kotlin和Gradle?