Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wongcheehong opened this issue May 24, 2022 · 15 comments
Labels
a: platform-views Embedding Android/iOS views in Flutter apps c: crash Stack traces logged to the console d: api docs Issues with https://api.flutter.dev/ engine flutter/engine repository. See also e: labels. found in release: 3.0 Found to occur in 3.0 found in release: 3.1 Found to occur in 3.1 has reproducible steps The issue has been confirmed reproducible and is ready to work on P2 Important issues not at the top of the work list platform-android Android applications specifically team-android Owned by Android platform team triaged-android Triaged by Android platform team

Comments

@wongcheehong
Copy link

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?

@XuanTung95
Copy link

I think the create method is changed between Flutter version.
Now it's create(p0: Context?, p1: Int, p2: Any?): PlatformView
Not create(context: Context, viewId: Int, args: Any?): PlatformView

@huycozy huycozy added the in triage Presently being triaged by the triage team label May 24, 2022
@huycozy
Copy link
Member

huycozy commented May 24, 2022

Hi @wongcheehong, thanks for filing the issue.
I see there's an open issue addressing the case you described.
Closing as duplicate of #98891, so please follow up on it for further updates.

@huycozy huycozy closed this as not planned Won't fix, can't repro, duplicate, stale May 24, 2022
@huycozy huycozy added r: duplicate Issue is closed as a duplicate of an existing issue and removed in triage Presently being triaged by the triage team labels May 24, 2022
@ilber
Copy link

ilber commented May 24, 2022

@huycozy I don't think this is a duplicate. The error here is because of this breaking change flutter/engine#31530 introduced in v3.0. It's a really bad change because the context is required to be notNull when dealing with Android views.

@huycozy huycozy reopened this May 24, 2022
@huycozy
Copy link
Member

huycozy commented May 24, 2022

Reopen this issue to investigate this issue further.
Btw, the quick fixes for this might be @XuanTung95's answer

Sample fixes
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)
    }
}
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)"
    }
}

@danagbemava-nc danagbemava-nc added in triage Presently being triaged by the triage team and removed r: duplicate Issue is closed as a duplicate of an existing issue labels May 24, 2022
@ilber
Copy link

ilber commented May 24, 2022

That would work if my native control would be a TextView. Unfortunately I am using a FrameLayout, and that one want's a context.

@huycozy
Copy link
Member

huycozy commented May 25, 2022

@ilber,
I can add FrameLayout with Context? as below:

Sample code
internal class MapView(context: Context?, id: Int, creationParams: Map<String?, Any?>?) : PlatformView {
    private lateinit var textView: TextView
    private lateinit var frameLayout: FrameLayout

    override fun getView(): View {
        return frameLayout
    }

    override fun dispose() {}

    init {
        context?.let { ct ->

            textView = TextView(ct)
            textView.textSize = 72f
            textView.text = "Rendered on a native Android view (id: $id)"

            frameLayout = FrameLayout(ct)
            val params = FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT)
            frameLayout.setBackgroundColor(Color.GREEN)
            frameLayout.addView(textView, params)
        }

    }
}
Demo

Screenshot_1653453849

@wongcheehong: you can try the above quick fixes as a temporary solution.

Anw, this issue is reproducible on latest stable and master channels with sample code in https://docs.flutter.dev/development/platform-integration/platform-views

flutter doctor -v
[✓] Flutter (Channel stable, 3.0.1, on macOS 12.2.1 21D62 darwin-x64, locale en-VN)
    • Flutter version 3.0.1 at /Users/huynq/Documents/GitHub/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision fb57da5f94 (8 hours ago), 2022-05-19 15:50:29 -0700
    • Engine revision caaafc5604
    • Dart version 2.17.1
    • DevTools version 2.12.2

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/huynq/Library/Android/sdk
    • Platform android-32, build-tools 31.0.0
    • ANDROID_HOME = /Users/huynq/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] Android Studio (version 4.1)
    • Android Studio at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/201.7042882/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)

[✓] Android Studio
    • Android Studio at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-1/203.7185775/Android Studio
      Preview.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6842174)

[✓] IntelliJ IDEA Community Edition (version 2020.3.3)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.67.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.40.0

[✓] Connected device (3 available)
    • iPhone 13 (mobile) • 2526BC1A-435D-4B08-B99C-44B928F2517B • ios            • com.apple.CoreSimulator.SimRuntime.iOS-15-4 (simulator)
    • macOS (desktop)    • macos                                • darwin-x64     • macOS 12.2.1 21D62 darwin-x64
    • Chrome (web)       • chrome                               • web-javascript • Google Chrome 101.0.4951.64

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
[✓] Flutter (Channel master, 3.1.0-0.0.pre.896, on macOS 12.2.1 21D62 darwin-x64, locale en-VN)
    • Flutter version 3.1.0-0.0.pre.896 at /Users/huynq/Documents/GitHub/flutter_master
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 09987dc00a (3 hours ago), 2022-05-24 18:48:10 -0700
    • Engine revision 7274f79325
    • Dart version 2.18.0 (build 2.18.0-149.0.dev)
    • DevTools version 2.13.1

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/huynq/Library/Android/sdk
    • Platform android-32, build-tools 31.0.0
    • ANDROID_HOME = /Users/huynq/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] Android Studio (version 4.1)
    • Android Studio at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/201.7042882/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)

[!] Android Studio
    • Android Studio at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-1/203.7185775/Android Studio Preview.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    ✗ Unable to find bundled Java version.
    • Try updating or re-installing Android Studio.

[✓] IntelliJ IDEA Community Edition (version 2020.3.3)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.67.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.40.0

[✓] Connected device (3 available)
    • sdk gphone x86 (mobile) • emulator-5554 • android-x86    • Android 11 (API 30) (emulator)
    • macOS (desktop)         • macos         • darwin-x64     • macOS 12.2.1 21D62 darwin-x64
    • Chrome (web)            • chrome        • web-javascript • Google Chrome 101.0.4951.64

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.

@huycozy huycozy added c: crash Stack traces logged to the console platform-android Android applications specifically a: platform-views Embedding Android/iOS views in Flutter apps has reproducible steps The issue has been confirmed reproducible and is ready to work on found in release: 3.0 Found to occur in 3.0 found in release: 3.1 Found to occur in 3.1 and removed in triage Presently being triaged by the triage team labels May 25, 2022
@ilber
Copy link

ilber commented May 25, 2022

Hey @huycozy , this is what I did in the end. Thanks a lot for helping us here. It doesn't look nice but it is a valid workaround. I was just curious to know why this change was introduced. With this change we cannot support older and newer versions of flutter.

@ilber
Copy link

ilber commented May 25, 2022

In order to be compatible also with older versions of Flutter, I have create a java class that extends PlatformViewFactory.

public class AndroidPlatformViewFactory extends PlatformViewFactory {
    public AndroidPlatformViewFactory() {
        super(StandardMessageCodec.INSTANCE);
    }

    @NonNull
    @Override
    public PlatformView create(Context context, int viewId, @Nullable Object args) {
        return new CustomNativeView(context);
    }
}

Here I just removed the @nullable annotation. everything works fine like this. It builds and works with both, old and new versions of Flutter.

@XuanTung95
Copy link

@huycozy I don't think this is a duplicate. The error here is because of this breaking change flutter/engine#31530 introduced in v3.0. It's a really bad change because the context is required to be notNull when dealing with Android views.

There is a chance @Nullable Context is a bug. We should ask the developer for confirmation.
cc @blasten

@ilber
Copy link

ilber commented May 25, 2022

I have already asked but no reply yet.

@blasten
Copy link

blasten commented May 26, 2022

This will be fixed in flutter/engine#33599

@wangmir
Copy link

wangmir commented May 31, 2022

In order to be compatible also with older versions of Flutter, I have create a java class that extends PlatformViewFactory.

public class AndroidPlatformViewFactory extends PlatformViewFactory {
    public AndroidPlatformViewFactory() {
        super(StandardMessageCodec.INSTANCE);
    }

    @NonNull
    @Override
    public PlatformView create(Context context, int viewId, @Nullable Object args) {
        return new CustomNativeView(context);
    }
}

Here I just removed the @nullable annotation. everything works fine like this. It builds and works with both, old and new versions of Flutter.

Can u elaborate more about this? I'm now struggling with this issue too, and I think u may be able to save me... How can i use this java to connect with former factory? Can i just extend AndroidPlatformViewFactory rather than PlatformViewFactory?

@ilber
Copy link

ilber commented May 31, 2022

Hey @wangmir , In the class I have pasted, you can just replace the CustomNativeView with your PlatformView and then you register the view factory as follows:

binding.platformViewRegistry.registerViewFactory(
                "id-of-the-platform-view",
                AndroidPlatformViewFactory()
            )

@wangmir
Copy link

wangmir commented May 31, 2022

Hey @wangmir , In the class I have pasted, you can just replace the CustomNativeView with your PlatformView and then you register the view factory as follows:

binding.platformViewRegistry.registerViewFactory(
                "id-of-the-platform-view",
                AndroidPlatformViewFactory()
            )

@ilber Oh, ok thanks I understood. Thanks. This issue saves me a lot..

flutter-painter added a commit to flutter-painter/blue_print_pos that referenced this issue Jun 24, 2022
@Waylon-Firework
Copy link

image

image

There is another place. In the flutter 2 version, there is a "?" here but not in flutter 3.

@flutter-triage-bot flutter-triage-bot bot added the d: api docs Issues with https://api.flutter.dev/ label Jul 5, 2023
@flutter-triage-bot flutter-triage-bot bot added multiteam-retriage-candidate team-android Owned by Android platform team triaged-android Triaged by Android platform team labels Jul 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: platform-views Embedding Android/iOS views in Flutter apps c: crash Stack traces logged to the console d: api docs Issues with https://api.flutter.dev/ engine flutter/engine repository. See also e: labels. found in release: 3.0 Found to occur in 3.0 found in release: 3.1 Found to occur in 3.1 has reproducible steps The issue has been confirmed reproducible and is ready to work on P2 Important issues not at the top of the work list platform-android Android applications specifically team-android Owned by Android platform team triaged-android Triaged by Android platform team
Projects
None yet
Development

No branches or pull requests