flutter与原生如何进行交互

flutter与原生如何进行交互

  • Flutter 主动调用原生
    • flutter端代码
    • Android端
    • IOS端
  • 原生如何主动调用Flutter
    • Android端
    • IOS端
    • Flutter处理原生调用

Flutter 主动调用原生

当flutter主动调用原生时,flutter与原生之间必须建立一个通道,然后通过此通道进行通信。

flutter端代码

/* 通道名称,必须与原生注册的一致*/
  static const flutter_to_native = const MethodChannel(
      'com.example.long:flutter_to_native');
static Future goNativeWithValue(String methodName,
     [Map map]) async {
   if (null == map) {//无参数
     dynamic future = await flutter_to_native.invokeMethod(methodName);//methodName方法名
     return future;
   } else {//带有参数
     dynamic future = await flutter_to_native.invokeMethod(methodName, map);
     return future;
   }
 }

Android端

       new MethodChannel(this.registrarFor("com.example.long:flutter_to_native").messenger(),"com.example.long:flutter_to_native")
        .setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
                if (Objects.equals(methodNames.get("login"), methodCall.method)){
                   //todo
                }
            }
        });

IOS端

 FlutterMethodChannel *flutterChannel = [FlutterMethodChannel methodChannelWithName:@"com.example.long:flutter_to_native" binaryMessenger:controller];
    [flutterChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
        if ([@"login" isEqualToString:call.method]) {//login应与flutter中所传的方法名一致
            NSString *userName = call.arguments[@"username"];//获取用户名密码
            NSString *password = call.arguments[@"password"];
            EMError* error = [[EMClient sharedClient] loginWithUsername:userName
                                                               password:password];
            if (!error) {
                 [[EMClient sharedClient].options setIsAutoLogin:YES];
                result(@(YES));//返回flutter端
            }else{
                result(@"请检查用户名或密码!");
            }
        }
   }

原生如何主动调用Flutter

Android端

        EventChannel eventChannel = new EventChannel(activity.registrarFor("com.example.long:native_to_flutter")
                .messenger(),"com.example.long:native_to_flutter");
        eventChannel.setStreamHandler(new EventChannel.StreamHandler() {
            @Override
            public void onListen(Object o, EventChannel.EventSink eventSink) {
                eventSink.success("原生传给Flutter值");
            }

            @Override
            public void onCancel(Object o) {
            }
        });
        

IOS端

FlutterEventChannel* eventChannel = [FlutterEventChannel eventChannelWithName:@"com.example.long:native_to_flutter" binaryMessenger:controller];
[eventChannel setStreamHandler:[[NativeToFlutterStream alloc] init]];

在NativeToFlutterStream中:

- (FlutterError *)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)events{
    _eventSink=events;
   _eventSink("原生传给Flutter值");
    return nil;
}

Flutter处理原生调用

static const native_to_flutter =
      const EventChannel('com.bhm.flutter.flutternb.plugins:native_to_flutter');
native_to_flutter.receiveBroadcastStream().listen((event){
      //todo
    },onError: (){
    },onDone: (){
    },);
      

你可能感兴趣的:(Flutter,Android,IOS)