Flutter使用FlutterPlatformView引发异常

时间:2019-01-28 05:00:46

标签: 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

这是颤振医生-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

**这在Android设备上是正常的!但是IOS没有显示此视图! **

1 个答案:

答案 0 :(得分:2)

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

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