首页 > 其他分享 >A named channel for communicating with platform plugins using asynchronous /// message passing.

A named channel for communicating with platform plugins using asynchronous /// message passing.

时间:2023-09-12 20:56:12浏览次数:36  
标签:named binaryMessenger communicating platform codec message null channel

 

  Future<void> initWithScopeLimitCredential() async {
    final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
        'dev.flutter.pigeon.CosApi.initWithScopeLimitCredential', codec,
        binaryMessenger: _binaryMessenger);
    final List<Object?>? replyList =
        await channel.send(null) as List<Object?>?;
    if (replyList == null) {
      throw PlatformException(
        code: 'channel-error',
        message: 'Unable to establish connection on channel.',
      );
    } else if (replyList.length > 1) {
      throw PlatformException(
        code: replyList[0]! as String,
        message: replyList[1] as String?,
        details: replyList[2],
      );
    } else {
      return;
    }
  }

  

 

Writing custom platform-specific code | Flutter https://docs.flutter.dev/platform-integration/platform-channels

/// A named channel for communicating with platform plugins using asynchronous
/// message passing.
///
/// Messages are encoded into binary before being sent, and binary messages
/// received are decoded into Dart values. The [MessageCodec] used must be
/// compatible with the one used by the platform plugin. This can be achieved
/// by creating a basic message channel counterpart of this channel on the
/// platform side. The Dart type of messages sent and received is [T],
/// but only the values supported by the specified [MessageCodec] can be used.
/// The use of unsupported values should be considered programming errors, and
/// will result in exceptions being thrown. The null message is supported
/// for all codecs.
///
/// The logical identity of the channel is given by its name. Identically named
/// channels will interfere with each other's communication.
///
/// All [BasicMessageChannel]s provided by the Flutter framework guarantee FIFO
/// ordering. Applications can assume messages sent via a built-in
/// [BasicMessageChannel] are delivered in the same order as they're sent.
///
/// See: <https://flutter.dev/platform-channels/>
class BasicMessageChannel<T> {
/// Creates a [BasicMessageChannel] with the specified [name], [codec] and [binaryMessenger].
///
/// The [name] and [codec] arguments cannot be null. The default [ServicesBinding.defaultBinaryMessenger]
/// instance is used if [binaryMessenger] is null.
const BasicMessageChannel(this.name, this.codec, { BinaryMessenger? binaryMessenger })
: _binaryMessenger = binaryMessenger;

/// The logical channel on which communication happens, not null.
final String name;

/// The message codec used by this channel, not null.
final MessageCodec<T> codec;

/// The messenger which sends the bytes for this channel.
///
/// On the root isolate or web, this defaults to the
/// [ServicesBinding.defaultBinaryMessenger]. In other contexts the default
/// value is a [BackgroundIsolateBinaryMessenger] from
/// [BackgroundIsolateBinaryMessenger.ensureInitialized].
BinaryMessenger get binaryMessenger {
final BinaryMessenger result = _binaryMessenger ?? _findBinaryMessenger();
return !kReleaseMode && debugProfilePlatformChannels
? _debugBinaryMessengers[this] ??= _ProfiledBinaryMessenger(
// ignore: no_runtimetype_tostring
result, runtimeType.toString(), codec.runtimeType.toString())
: result;
}
final BinaryMessenger? _binaryMessenger;

/// Sends the specified [message] to the platform plugins on this channel.
///
/// Returns a [Future] which completes to the received response, which may
/// be null.
Future<T?> send(T message) async {
return codec.decodeMessage(await binaryMessenger.send(name, codec.encodeMessage(message)));
}

/// Sets a callback for receiving messages from the platform plugins on this
/// channel. Messages may be null.
///
/// The given callback will replace the currently registered callback for this
/// channel, if any. To remove the handler, pass null as the `handler`
/// argument.
///
/// The handler's return value is sent back to the platform plugins as a
/// message reply. It may be null.
void setMessageHandler(Future<T> Function(T? message)? handler) {
if (handler == null) {
binaryMessenger.setMessageHandler(name, null);
} else {
binaryMessenger.setMessageHandler(name, (ByteData? message) async {
return codec.encodeMessage(await handler(codec.decodeMessage(message)));
});
}
}

// Looking for setMockMessageHandler?
// See this shim package: packages/flutter_test/lib/src/deprecated.dart
}

 

 

 

 

 

翻译

搜索

复制

标签:named,binaryMessenger,communicating,platform,codec,message,null,channel
From: https://www.cnblogs.com/papering/p/17697781.html

相关文章

  • Unity 性能优化之Shader分析处理函数 ShaderUtil.GetAvailableShaderCompilerPlatform
    Unity性能优化之Shader分析处理函数ShaderUtil.GetAvailableShaderCompilerPlatforms用法点击封面跳转到Unity国际版下载页面简介在Unity中,性能优化是游戏开发过程中非常重要的一环。其中,ShaderUtil.GetAvailableShaderCompilerPlatforms函数是一个内部函数,它可以帮助......
  • pip install ale_python_interface 安装报错,ModuleNotFoundError: No module named 'a
    参考:https://www.cnblogs.com/hasakei/p/10035198.htmlhttps://blog.csdn.net/senjie_wang/article/details/84073823https://github.com/bbitmaster/ale_python_interface/issues/2https://blog.csdn.net/dream6985/article/details/127746687  ======================......
  • No module named 'sklearn'解决方案
    sklearn深度学习库官方网站,打开之后按需复制命令进行安装,此处只列出两个最常用的:windows下pip安装:pipinstall-Uscikit-learnLinux下pip安装:pip3install-Uscikit-learnwindows/linux下conda安装:condainstall-cconda-forgescikit-learn ......
  • 20.2 设备树中的 platform 驱动编写
    一、设备树下的platform驱动  platform驱动框架分为总线、设备和驱动,总线不需要我们去管理,这个是Linux内核提供。在有了设备树的前提下,我们只需要实现platform_driver即可。 1. 修改pinctrl-stm32.c文件   先复习一下pinctrl子系统和gpio子系统,pinctrl子......
  • 第4篇 machine,platform,codec
    参考:韦东山Linux教程(1)machine:单板相关 //结构体:structsnd_soc_card->structsnd_soc_dai_link 1)表明platform是哪个,通过snd_soc_dai_link指定名字,进行匹配 1.指定CPUDAI(IIS) 2.snd_soc_platform_driver(指定DMA) 2)表明codec是哪个,通过s......
  • ModuleNotFoundError: No module named 'flask._compat’
    原因:flask版本过高,flask里面的._compat.py文件没有;解决方案1:解决:降低flask版本,1.1.2即可pipinstallflask==1.1.2 解决方案2:更改flask_script文件,Ctrl+左键 进入到flask_script的 __init__.py 源码文件中,将 fromflask._compatimporttext_type 语句改成 fromf......
  • ModuleNotFoundError: No module named 'torch._six'
    ModuleNotFoundError:Nomodulenamed'torch._six' ---------------------------------由于torch2.0版本没有这个模块,因此,只需要降低torch版本,就可以解决。1)新建一个虚拟环境2)安装低版本torch REFhttps://blog.csdn.net/wzwddkb/article/details/130400156-------......
  • WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platfo
    问题描述使用start-dfs.sh命令开启hdfs服务时,爆出这样的警告信息问题解决可以先进入到我们下载hadoop的文件目录下,然后进入到这个文件里面:vimetc/hadoop/log4j.properties然后将下面的语句添加到问年末尾处:log4j.logger.org.apache.hadoop.util.NativeCodeLoader=ERROR......
  • platform_device_register和platform_device_register_full的区别
    platform_device_register和platform_device_register_full都是用于在Linux内核中注册平台设备的函数,但是它们之间存在一些区别。platform_device_registerplatform_device_register函数用于注册一个平台设备。它接受一个指向platform_device结构体的指针作为参数,该结构体表示......
  • SpringBoot复习:(49)NamedParameterJdbcTemplate用法
    packagecn.edu.tju.controller;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.autoconfigure.web.ServerProperties;importorg.springframework.boot.context.properties.EnableConfigurationProperties;importorg......