android - Flutter 使用 FlutterPlatformView 抛出异常

标签 android ios flutter

使用Flutter开发IOS时,使用FlutterPlatformView抛出异常

异常信息如下:

2019-01-28 12:38:47.804217+0800 Runner[10334:1416396][VERBOSE-2:platform_view_layer.cc(19)] Trying to embed a platform
view but the PrerollContext does not support embedding

这是 flutter doctor -v 的结果:

[✓] Flutter (Channel beta, v1.0.0, on Mac OS X 10.14 18A391, locale zh-Hans-CN)
    • Flutter version 1.0.0 at /Users/chenshuang/flutter
    • Framework revision 5391447fae (8 weeks ago), 2018-11-29 19:41:26 -0800
    • Engine revision 7375a0f414
    • Dart version 2.1.0 (build 2.1.0-dev.9.4 f9ebf21297)

[!] Android toolchain - develop for Android devices (Android SDK 28.0.3)
    • Android SDK at /Users/chenshuang/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling
      support)
    • Platform android-28, build-tools 28.0.3
    • ANDROID_HOME = /Users/chenshuang/Library/Android/sdk
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor
      --android-licenses

[!] iOS toolchain - develop for iOS devices (Xcode 10.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 10.1, Build version 10B61
    ✗ libimobiledevice and ideviceinstaller are not installed. To install with
      Brew, run:
        brew update
        brew install --HEAD usbmuxd
        brew link usbmuxd
        brew install --HEAD libimobiledevice
        brew install ideviceinstaller
    ✗ ios-deploy not installed. To install with Brew:
        brew install ios-deploy
    ✗ Brew can be used to install tools for iOS device development.
      Download brew at https://brew.sh/.

[✓] Android Studio (version 3.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 29.0.1
    • Dart plugin version 173.4700
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)

[✓] Connected device (1 available)
    • ONEPLUS A5010 • c738aafb • android-arm64 • Android 8.1.0 (API 27)

这是 AppDelegate:

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    IOSTextViewPlugin.registerWith(pluginRegistrar: registrar(forPlugin: "IOSTextViewPlugin"))
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

这是 IOSTextViewPlugin:

import Flutter

class IOSTextViewPlugin{

    public static func registerWith(pluginRegistrar: FlutterPluginRegistrar){
        let factory = IOSTextViewPlatformTextViewFactory.init(messenger: pluginRegistrar.messenger())
        pluginRegistrar.register(factory, withId: "IOSTextViewPlugin")
    }
}

这是 IOSTextViewPlatformTextViewFactory:

import Flutter

class IOSTextViewPlatformTextViewFactory:NSObject,FlutterPlatformViewFactory{

    private let messenger: FlutterBinaryMessenger

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

    func create(withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?) -> FlutterPlatformView {
        return IOSTextView.init(messenger: messenger, frame: frame, viewId: viewId, args: args)
    }
}

这是 IOSTextView:

import Flutter

class IOSTextView :NSObject,FlutterPlatformView{

    private let frame: CGRect
    private let viewId: Int64
    private let messenger: FlutterBinaryMessenger
    private var uiLabel: UILabel

    init(messenger: FlutterBinaryMessenger,frame: CGRect, viewId: Int64, args: Any?){
        self.messenger = messenger
        self.frame = frame
        self.viewId = viewId

        uiLabel = UILabel.init(frame: frame)
        uiLabel.text = "UILabel"
    }

    func view() -> UIView {
        return uiLabel
    }

}

这是飞镖:

class BaiduMapAppState extends State<BaiduMapApp> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      title: "Platform View Test",
      home: Scaffold(
        appBar: AppBar(
          title: Text('Platform View Test'),
        ),
        body: Center(
            child: SizedBox(
                width: 100,
                height: 100,
                child: defaultTargetPlatform == TargetPlatform.android
                    ? AndroidView(
                  viewType: 'AndroidTextViewPlugin',
                  creationParams: {"content": "通过参数传入的文本内容"},
                  creationParamsCodec: const StandardMessageCodec(),
                  onPlatformViewCreated: onMyViewCreated,
                )
                    : UiKitView(viewType: 'IOSTextViewPlugin',)
            )),
      ),
    );
  }

这是 Info.plist:

enter image description here

**在安卓上是正常的!但是IOS不显示这个 View ! **

最佳答案

您需要通过在您的 .plist 文件中添加以下内容来加入对 iOS 的 PlatformView 支持:

<key>io.flutter.embedded_views_preview</key>
<true/>

关于android - Flutter 使用 FlutterPlatformView 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54395776/

相关文章:

flutter - 如何重置我的重置此应用程序的当前状态

android - 如何在 Flutter 中让 alertDialog 在几秒钟后自动消失?

android - 如何在应用程序未运行时显示通知

java - 解析包含多个嵌套对象的 JSON 对象而不为每个嵌套对象创建类

android studio 错误 : app-release-unsgined. apk 未签名

iOS 8.3 共享扩展 - 启动 URL 方案

android - 如何初始化上下文?

ios - 从获取 API 响应的视频 url 获取/制作缩略图

ios - 模糊效果未正确调整到父 View

flutter - 使用 Flutter intl 扩展在 Flutter 中组织 .arb 文件