原生混合开发flutter Unhandled Exception: MissingPluginException(No implementation found for method ....

这是一个通用的问题,不单单指定某一个插件方法不支持的问题。

当然前面也说一下,你可以先试一下清除重新加载看一下问题能不能解决,有的时候是你的项目没有restart,热重启不会重新编译新加入的资源。

纯flutter:quit项目->(flutter clean 可不执行)-> flutter run

module混合:module项目flutter pub get -> 原生项目pod install ->再运行启动项目

如果上面能够解决你的问题,那么下面就不用看了。

今天混合开发iOS混合之前自己写的屏幕旋转插件:limiting_direction_csx 由于公司一直使用纯flutter开发,使用这个插件没有一点问题,近期之前的朋友看到我的这个插件,他们公司也在尝试使用flutter开发,就采用了原生与flutter混合的方式,其中就涉及到了屏幕旋转支持问题,他就搜到了我的这个插件,结果在使用过程中就遇到了Unhandled Exception: MissingPluginException(No implementation found for method ....。插件的方法都没有的情况!朋友瞬间就觉得我是一个骗子了,百般质疑我。好无奈,谁让咱自己没有考虑到module形式的问题呢,哈哈。

结果就反馈到了我这里,我就写了一个混合demo,自己尝试了一下,发现的确是所有的方法都不支持?找不到?但是运行module里面的iOS项目没问题,正常屏幕旋转。

后来在github上面参考这个截图获得了灵感:

其中第四条就是我们遇到的问题所在,就是我们module形式混合默认情况下flutter module的其他与原生互动的插件不会被重新注册到原生的项目中去,那么我们就需要手动的方式来将这些插件注册一下,以联通flutter module里面的桥接和主项目的桥接。可以仿照纯flutter的依赖appdelegate.m里面的注册写法:

iOS方面:


#import "ViewController.h"
#import <Flutter/Flutter.h>
#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h>
#import <limiting_direction_csx/MainViewController.h>
#import "OCViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (IBAction)goFlutterAction:(UIButton *)sender {
    //这里由于屏幕旋转的支持需要将FlutterViewController替换成MainViewController
    MainViewController *flutterViewController = [MainViewController new];
//注册我们的module里面的桥接
    [GeneratedPluginRegistrant registerWithRegistry:flutterViewController];
    [flutterViewController setInitialRoute:@"flutter_login"];
    [self.navigationController pushViewController:flutterViewController animated:YES];
    
    __weak typeof(self) weakSelf = self;
    FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"com.allen.test.call" binaryMessenger:flutterViewController];
    [channel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
    // Flutter invokeMethod 特定方法的时候会触发到这里
        if ([call.method isEqualToString:@"getFlutterMessage"]) {
            result(@"接收到flutter的消息,回传信息from OC");
            NSLog(@"接收到flutter的参数:%@",call.arguments);
            [weakSelf goOCVC:call.arguments];
        }
    }];
}

- (void)goOCVC:(NSString *)flutterStr{
    OCViewController *ocVC = [[OCViewController alloc]init];
    ocVC.flutterMsg = flutterStr;
    [self.navigationController pushViewController:ocVC animated:YES];
}


@end

安卓方面(由于自己是些iOS的对安卓不太熟练,这里就不上代码误人子弟了,提供一个方向,代码应该写起来也不是很麻烦):

。。。。。

[GeneratedPluginRegistrant registerWithRegistry:flutterViewController]; 不要在AppDelegate.m去注册,混编的话就是在进入每个flutter页面前去注册。

奉上我的demo链接,可以尝试:https://github.com/KirstenDunst/flutter_integrated_demo.git

猜你喜欢

转载自blog.csdn.net/BUG_delete/article/details/115342517