gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:23:25 28 4
gpt4 key购买 nike

按照 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.PlatformViewFactorye: 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com