Flutter開發(fā)填坑記錄

1.錯誤提示

2019-08-01 18:57:29.445804+0800 pedi Dev[48916:362606] [VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: PlatformException(unregistered_view_type, trying to create a view with an unregistered type, unregistered view type: 'plugins.flutter.io/webview')

#0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564:7)

#1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)

#2 PlatformViewsService.initUiKitView (package:flutter/src/services/platform_views.dart:167:41)

#3 _UiKitViewState._createNewUiKitView (package:flutter/src/widgets/platform_view.dart:499:71)

#4 _UiKitViewState._initializeOnce (package:flutter/src/widgets/platform_view.dart:449:5)

#5 _UiKitViewState.didChangeDependencies (package:flutter/src/widgets/platform_view.dart:459:5)

webview加載失敗說明兩個問題:

1.plugin注冊失敗

2.ios工程在info.plist 需要加

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

如果url是http,還需要加

<key>NSAppTransportSecurity</key>

? ? ? ? ? <key>NSAllowsArbitraryLoads</key>

? ? ? ? <true/>

? ? ? ? <key>NSAllowsArbitraryLoadsInWebContent</key>

? ? ? ? <true/>

iOS Platform

FlutterAppDelegate,是iOS的Plugin管理器,它記錄了所有的Plugin,并將Plugin綁定到FlutterViewController(默認是rootViewController)。官方Demo是個StoryBoard,以及純flutter項目他們的RootViewController默認就是FlutterViewController。而Plugin注冊很重要的一點是,只有注冊到FlutterViewController,才能注冊成功,才可以加載執(zhí)行Flutter第三方插件。否則就會出現(xiàn)上述問題。而混合開發(fā)的工程絕大多數(shù)的RootViewController都不可能是FlutterViewController,一定是個Native的頁面比如TabbarController或者NavigationController,所以注冊一定失敗。所以,如果出現(xiàn)這種情況,下面是閑魚,以及官方的注冊Plugin的代碼教程。

Google官方Flutter混合工程集成文檔的Appdelegate改造


閑魚推薦方法

FlutterViewController* flutterViewController = [[FlutterViewController alloc] initWithEngine:self.engine nibName:nil bundle:nil];?

self.flutterEngine= [[FlutterEnginealloc]initWithName:@"io.flutter"project:nil];? [self.flutterEnginerunWithEntrypoint:nil];? [GeneratedPluginRegistrantregisterWithRegistry:self.flutterEngine];

經(jīng)過多次運行測試,發(fā)現(xiàn)這三行代碼竟然會有時效性的問題。比如老工程的didFinishLaunchingWithOptions方法里面 一定有很多的第三方模塊的初始化代碼 以及自己工程的網(wǎng)絡工具初始化等各個模塊的代碼,所以中間的耗時操作,會影響到上面的Plugin注冊。經(jīng)常創(chuàng)建失敗。 用flutterengine 去注冊 給appdelegate 加一個flutterengine的屬性。然后創(chuàng)建fluttervc也要用initwithengine 的方式。你以為直接運行就完事了?然并卵。 運行白屏。使用延遲加載也不管用,網(wǎng)上搜索到的教程

經(jīng)過大神指點要先使用present然后dismiss才能顯示出來

__weak __typeof(self)weakSelf = self;? ? ? ? self.ctr4.modalPresentationStyle = UIModalPresentationOverCurrentContext;? ? ? ? [self presentViewController:weakSelf.ctr4 animated:NO completion:^{? ? ? ? ? ? [weakSelf dismissViewControllerAnimated:NO completion:^{? ? ? ? ? ? ? ? [weakSelf addChildViewController:weakSelf.ctr4];? ? ? ? ? ? ? ? [weakSelf.view bringSubviewToFront:weakSelf.tabbarContainer];? ? ? ? ? ? }];? ? ? ? }];

接下來準備添加多個flutter

然而在push過程中發(fā)現(xiàn)flutter的第一次顯示的界面竟然是上次tab的頁面,因為engine是同一份的,我們創(chuàng)建的時候會保存一份engine。

路由傳值我們之前會用到- (void)setInitialRoutez:(NSString*)route ,但是用Engine注冊你會發(fā)現(xiàn)傳入到main.dart里面的window.defaultRouteName一直是 / 根目錄符號,這時候,Native初始化Flutter頁面的代碼以及傳后續(xù)請求頭的路無情中被堵的死死的。所以你只能用消息發(fā)送的機制來解決多路由傳值的功能,要用到FlutterMethodChannel,EventChannel,BasicMessageChannel,雖然很常用,但是對于初始化路由來講,過于繁雜,復雜度高。

最后的最后。經(jīng)過了無數(shù)次,復雜,漫長,絕望,心灰意冷的嘗試后。

發(fā)現(xiàn)? 你只需要把這行代碼?

[GeneratedPluginRegistrant registerWithRegistry:self];

放到你需要被插件響應的FlutterViewController頁面里面,就解決了上面所有的問題。


做了最近一段時間flutter的理解就是。flutter他整個底層還是不夠完善不夠充足。就相當于。你從點A到B。他只有一條或者兩條路,

你只能走著走著,前面有個坑,你跳進去,然后再爬上了,再往前走,再跳進去再爬上來。就像實現(xiàn)一個功能。他只能那么一個或者兩個方法才可以實現(xiàn),你沒法繞過或者通過別的實現(xiàn)方式去做,就是你只能這樣搞而不是說像現(xiàn)在比較成熟的一些比如說IOS,安卓或者是H5。他整個從A到B從這走到那兒。走不過去,你可以換條別的路過去。就這些不夠豐富的api是flutter底層比較欠缺的,不夠完備的地方,

這也是任何一門語言的必經(jīng)之路,隨著開發(fā)者的不斷加入,牽線搭橋,疊加最偉大公司Google 的背書。整個flutter還是會馬不停蹄的發(fā)展壯大的。


文章參考:

http://www.itdecent.cn/p/5e5d54db8c7e? Flutter和原生iOS交互(GA_

http://www.itdecent.cn/p/119fc6873434? Flutter Plugin 調(diào)用 Native APIs(閑魚技術)

https://juejin.im/post/5c6e84156fb9a049a5718047? flutter 多實例實戰(zhàn)(共田君)

https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps? Google自家混合集成Wiki

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容