Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter throws an exception using FlutterPlatformView

When using Flutter to develop IOS, throw an exception using FlutterPlatformView.

The exception information is as follows:

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

This is the result of 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)

This is 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)
  }
}

This is IOSTextViewPlugin:

import Flutter

class IOSTextViewPlugin{

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

This is 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)
    }
}

This is 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
    }

}

This is Dart:

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',)
            )),
      ),
    );
  }

This is Info.plist:

enter image description here

**It’s normal on Android! But IOS does not show this view! **

like image 546
Jam Mr Avatar asked Jan 28 '19 05:01

Jam Mr


1 Answers

You need to op-in the PlatformView support for iOS by adding below in your .plist file:

<key>io.flutter.embedded_views_preview</key>
<true/>
like image 134
SuperBear Avatar answered Oct 23 '22 07:10

SuperBear