美文网首页Flutter
BasicMessageChannel

BasicMessageChannel

作者: hypercode | 来源:发表于2023-07-10 15:37 被阅读0次

    前言:BasicMessageChannel是flutter官方的一个通讯手段,用于混合工程中flutter和原生的通讯。
    用法:
    iOS端:

    FBFlutterViewContainer * fvc  = [[FBFlutterViewContainer alloc]init];
    [fvc setName:@"FollowingPage" uniqueId:nil params:nil opaque:true];
    [weakSelf.navigationController pushViewController:fvc animated:YES];
    [UIApplication sharedApplication].keyWindow.rootViewController;
            FlutterBasicMessageChannel* messageChannel = [FlutterBasicMessageChannel messageChannelWithName:@"flutter_and_native_channel_name" binaryMessenger:fvc.binaryMessenger];
    [messageChannel sendMessage:@{@"flutter messag":@"native向flutter发送了参数\n"}];
    

    Fluttter端:

    static const messageChannel =
          BasicMessageChannel('flutter_and_native_channel_name', StandardMessageCodec());
    
    channel.setMessageHandler((message) async{
      // 处理接收到的消息
        print('Received message: $message');
        return " ";
    });
    

    其中flutter端的接收也可以放到main里,说明不一定是跳转过来的页才能接收消息。

    相关文章

      网友评论

        本文标题:BasicMessageChannel

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