flutter與原生如何進行交互

Flutter 主動調用原生

當flutter主動調用原生時,flutter與原生之間必須建立一個通道,然後通過此通道進行通信。

flutter端代碼

/* 通道名稱,必須與原生註冊的一致*/
  static const flutter_to_native = const MethodChannel(
      'com.example.long:flutter_to_native');
static Future<dynamic> goNativeWithValue(String methodName,
     [Map<String, dynamic> 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: (){
    },);
      
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章