Flutter开发填坑记录

作者: W____Y | 来源:发表于2019-08-16 00:08 被阅读0次

    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,才能注册成功,才可以加载执行Flutter第三方插件。否则就会出现上述问题。而混合开发的工程绝大多数的RootViewController都不可能是FlutterViewController,一定是个Native的页面比如TabbarController或者NavigationController,所以注册一定失败。所以,如果出现这种情况,下面是闲鱼,以及官方的注册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];

    经过多次运行测试,发现这三行代码竟然会有时效性的问题。比如老工程的didFinishLaunchingWithOptions方法里面 一定有很多的第三方模块的初始化代码 以及自己工程的网络工具初始化等各个模块的代码,所以中间的耗时操作,会影响到上面的Plugin注册。经常创建失败。 用flutterengine 去注册 给appdelegate 加一个flutterengine的属性。然后创建fluttervc也要用initwithengine 的方式。你以为直接运行就完事了?然并卵。 运行白屏。使用延迟加载也不管用,网上搜索到的教程

    经过大神指点要先使用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过程中发现flutter的第一次显示的界面竟然是上次tab的页面,因为engine是同一份的,我们创建的时候会保存一份engine。

    路由传值我们之前会用到- (void)setInitialRoutez:(NSString*)route ,但是用Engine注册你会发现传入到main.dart里面的window.defaultRouteName一直是 / 根目录符号,这时候,Native初始化Flutter页面的代码以及传后续请求头的路无情中被堵的死死的。所以你只能用消息发送的机制来解决多路由传值的功能,要用到FlutterMethodChannel,EventChannel,BasicMessageChannel,虽然很常用,但是对于初始化路由来讲,过于繁杂,复杂度高。

    最后的最后。经过了无数次,复杂,漫长,绝望,心灰意冷的尝试后。

    发现  你只需要把这行代码 

    [GeneratedPluginRegistrant registerWithRegistry:self];

    放到你需要被插件响应的FlutterViewController页面里面,就解决了上面所有的问题。

    做了最近一段时间flutter的理解就是。flutter他整个底层还是不够完善不够充足。就相当于。你从点A到B。他只有一条或者两条路,

    你只能走着走着,前面有个坑,你跳进去,然后再爬上了,再往前走,再跳进去再爬上来。就像实现一个功能。他只能那么一个或者两个方法才可以实现,你没法绕过或者通过别的实现方式去做,就是你只能这样搞而不是说像现在比较成熟的一些比如说IOS,安卓或者是H5。他整个从A到B从这走到那儿。走不过去,你可以换条别的路过去。就这些不够丰富的api是flutter底层比较欠缺的,不够完备的地方,

    这也是任何一门语言的必经之路,随着开发者的不断加入,牵线搭桥,叠加最伟大公司Google 的背书。整个flutter还是会马不停蹄的发展壮大的。

    文章参考:

    https://www.jianshu.com/p/5e5d54db8c7e  Flutter和原生iOS交互(GA_

    https://www.jianshu.com/p/119fc6873434  Flutter Plugin 调用 Native APIs(闲鱼技术)

    https://juejin.im/post/5c6e84156fb9a049a5718047  flutter 多实例实战(共田君)

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

    相关文章

      网友评论

        本文标题:Flutter开发填坑记录

        本文链接:https://www.haomeiwen.com/subject/cejxsctx.html