Skip to content

IOSPlatformView: document that the [FlutterPlatformView view] should not return a new instance every time its called #56736

@cyanglaz

Description

@cyanglaz

The view API is declared here:
https://github.com/flutter/engine/blob/master/shell/platform/darwin/ios/framework/Headers/FlutterPlatformViews.h#L22
After we implemented the unobstructed platform view. We need the embedded view to be cached. As a result, the platform view implementer must not create a new instance of the platform view every frame.
It was never a good practice anyway as it reduces performance.
One example of good implementation of platform view would be:

class UIRectView: NSObject, FlutterPlatformView {
    let embedded_view: UIView

    init(_ frame: CGRect) {
        embedded_view = UIView(frame: frame)
        embedded_view.backgroundColor = UIColor.cyan
    }

    func view() -> UIView {
        return embedded_view
    }
}

A bad example would be:

class UIRectView: NSObject, FlutterPlatformView {
    func view() -> UIView {
        let embedded_view = UIView(frame: frame)
        embedded_view.backgroundColor = UIColor.cyan
        return embedded_view;
    }
}

We should document the same in the public API.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projecta: platform-viewsEmbedding Android/iOS views in Flutter appsd: api docsIssues with https://api.flutter.dev/frameworkflutter/packages/flutter repository. See also f: labels.platform-iosiOS applications specificallyteam-iosOwned by iOS platform teamtriaged-iosTriaged by iOS platform team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions