Blu*_*lue 3 android kotlin flutter
按照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): 类 'MapViewFactory' 不是抽象的,而是抽象的不实现抽象基类成员 public Abstract fun create(p0: Context?, p1: Int, p2: Any?): PlatformView 定义在 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' 不覆盖任何内容
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())
}
}
Run Code Online (Sandbox Code Playgroud)
地图视图.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)"
}
}
Run Code Online (Sandbox Code Playgroud)
地图视图工厂
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)
}
}
Run Code Online (Sandbox Code Playgroud)
这是 Flutter 方面的错误吗?
小智 6
这是 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)
}
}
Run Code Online (Sandbox Code Playgroud)
更改源代码
override fun create(context: Context, viewId: Int, args: Any?):
Run Code Online (Sandbox Code Playgroud)
到
override fun create(context: Context?, viewId: Int, args: Any?):
/// you are missing '?' from 'context:Context'
Run Code Online (Sandbox Code Playgroud)
从细节可以看到PlatformViewFactory源码:
public abstract PlatformView create(@Nullable Context context, int viewId, @Nullable Object args);
Run Code Online (Sandbox Code Playgroud)
上下文可以为空 - 所以你缺少“?” 在 Kotlin 空安全中
归档时间: |
|
查看次数: |
3958 次 |
最近记录: |