dart 无法向Flutter平台视图Swift `UiKitView`传递参数

zujrkrfu  于 2023-01-17  发布在  Flutter
关注(0)|答案(1)|浏览(150)

我正在使用Platform View在Flutter应用程序中集成原生视图。我成功地集成了Android视图,但我在iOS视图中遇到了问题。我完全按照Flutter docs中的说明:Hosting native iOS views in your Flutter app with Platform Views,但我的参数不知何故无法传递给Swift native。
dart 代码:

UiKitView(
  viewType: '<platform-view-type>',
  layoutDirection: ui_import.TextDirection.ltr,
  creationParams: {"num": 1, "text": "example"}, // my argument
  creationParamsCodec: StandardMessageCodec(),
)

Swift代码,FLNativeView.swift

class FLNativeViewFactory: NSObject, FlutterPlatformViewFactory {
    private var messenger: FlutterBinaryMessenger

    init(messenger: FlutterBinaryMessenger) {
        self.messenger = messenger
        super.init()
    }

    func create(
        withFrame frame: CGRect,
        viewIdentifier viewId: Int64,
        arguments args: Any?
    ) -> FlutterPlatformView {
        return FLNativeView( // <---- breakpoint here, but args parameter is nil
            frame: frame,
            viewIdentifier: viewId,
            arguments: args,
            binaryMessenger: messenger)
    }
}

我的AppDelegate.swift

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        GeneratedPluginRegistrant.register(with: self)
        
        weak var registrar = self.registrar(forPlugin: "plugin-name")
        
        let factory = FLNativeViewFactory(messenger: registrar!.messenger())
        self.registrar(forPlugin: "<plugin-name>")!.register(
            factory,
            withId: "<platform-view-type>")
        
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}

我的flutter doctor

[✓] Flutter (Channel stable, 3.7.8, on macOS 13.4 22F66 darwin-arm64, locale en-VN)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.1)
[✓] VS Code (version 1.78.2)
[✓] Connected device (3 available)
[✓] HTTP Host Availability

P/s:我必须在原生视图上工作的原因是Flutter的PDF功能不像Kotlin/Swift那样“完整”。
我完全按照Flutter文档中的指示,也查看了其他Flutter帖子,Android工作,但iOS不。

mrfwxfqh

mrfwxfqh1#

该死的,我想出来了!问题是缺乏文件。这个问题在GitHub上出现了:https://github.com/flutter/flutter/issues/28124。在我的FLNativeViewFactory中,添加以下函数:

public func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
          return FlutterStandardMessageCodec.sharedInstance()
    }

然后它工作

相关问题