Flutter typedef 函数类型

 typedef 函数 在flutter中的应用:

实际上在使用 flutter 的时候经常会用到这个东西:比如 IconButton 组件里面  onPressed

IconButton(onPressed: () {},icon: Icon(Icons.add),
),

到 onPressed 定义的地方:  VoidCallback

/// The callback that is called when the button is tapped or otherwise activated.
///
/// If this is set to null, the button will be disabled.
final VoidCallback? onPressed;

 到 VoidCallback 定义的地方:   发现是一个  typedef 类型

/// Signature of callbacks that have no arguments and return no data.
typedef VoidCallback = void Function();

其实 flutter 还有很多类型的东西:

typedef VoidCallback        = void Function();
typedef ValueChanged<T>     = void Function(T value);
typedef ValueSetter<T>      = void Function(T value);
typedef ValueGetter<T>      = T Function();
typedef IterableFilter<T>   = Iterable<T> Function(Iterable<T> input);
typedef AsyncCallback       = Future<void> Function();
typedef AsyncValueSetter<T> = Future<void> Function(T value);

上面都是 Flutter 自带的一些 typedef 函数,当然你也可以自定义。不过使用他自带的其实也都够用了!

typedef Compare<T> = int Function(T a, T b);

因为 typedef 是一个类型, 所以他可以使用 is 方法判断函数是不是你自己定义的:

typedef Compare<T> = int Function(T a, T b);int sort(Object a, Object b) => 0;class SortedCollection {Compare compare;SortedCollection(this.compare);
}void main() {SortedCollection coll = SortedCollection(sort);printf(coll.compare is Function); //falseprintf(coll.compare is Compare);  //TRUE
}
typedef 自定义函数

自定义其他 多参数的情况:

typedef VoidCallback = void Function();
typedef ErrorCallback = void Function(int code, String error);
typedef V2TimUserFullInfoCallback = void Function(V2TimUserFullInfo info);
typedef OnTotalUnreadMessageCountChanged = void Function(int totalUnreadCount);
typedef OnUserStatusChanged = void Function(List<V2TimUserStatus> userStatusList);
typedef OnLog = void Function(int logLevel, String logContent);
typedef OnRecvC2CTextMessageCallback = void Function(String msgID,V2TimUserInfo userInfo,String text,
);
typedef OnRecvMessageExtensionsChanged = void Function(String msgID,List<V2TimMessageExtension> extensions,
);
typedef OnRecvMessageExtensionsDeleted = void Function(String msgID,List<String> extensionKeys,
);
typedef OnMessageDownloadProgressCallback = void Function(V2TimMessageDownloadProgress messageProgress,
);
typedef OnRecvC2CCustomMessageCallback = void Function(String msgID,V2TimUserInfo sender,String customData,
);
typedef OnRecvGroupTextMessageCallback = void Function(String msgID,String groupID,V2TimGroupMemberInfo sender,String text,
);typedef OnRecvGroupCustomMessageCallback = void Function(String msgID,String groupID,V2TimGroupMemberInfo sender,String customData,
);
typedef OnMemberEnterCallback = void Function(String groupID,List<V2TimGroupMemberInfo> memberList,
);
// void 	onMemberEnter (String groupID, List< V2TIMGroupMemberInfo > memberList)
typedef OnMemberLeaveCallback = void Function(String groupID,V2TimGroupMemberInfo member,
);
// void 	onMemberLeave (String groupID, V2TIMGroupMemberInfo member)
typedef OnMemberInvitedCallback = void Function(String groupID,V2TimGroupMemberInfo opUser,List<V2TimGroupMemberInfo> memberList,
);
// void 	onMemberInvited (String groupID, V2TIMGroupMemberInfo opUser, List< V2TIMGroupMemberInfo > memberList)
typedef OnMemberKickedCallback = void Function(String groupID,V2TimGroupMemberInfo opUser,List<V2TimGroupMemberInfo> memberList,
);
// void 	onMemberKicked (String groupID, V2TIMGroupMemberInfo opUser, List< V2TIMGroupMemberInfo > memberList)
typedef OnMemberInfoChangedCallback = void Function(String groupID,List<V2TimGroupMemberChangeInfo> v2TIMGroupMemberChangeInfoList,
);
// void 	onMemberInfoChanged (String groupID, List< V2TIMGroupMemberChangeInfo > v2TIMGroupMemberChangeInfoList)
typedef OnGroupCreatedCallback = void Function(String groupID,
);
// void 	onGroupCreated (String groupID)
typedef OnGroupDismissedCallback = void Function(String groupID,V2TimGroupMemberInfo opUser,
);
// void 	onGroupDismissed (String groupID, V2TIMGroupMemberInfo opUser)
typedef OnGroupRecycledCallback = void Function(String groupID,V2TimGroupMemberInfo opUser,
);
// void 	onGroupRecycled (String groupID, V2TIMGroupMemberInfo opUser)
typedef OnGroupInfoChangedCallback = void Function(String groupID,List<V2TimGroupChangeInfo> changeInfos,
);
// void 	onGroupInfoChanged (String groupID, List< V2TIMGroupChangeInfo > changeInfos)
typedef OnReceiveJoinApplicationCallback = void Function(String groupID,V2TimGroupMemberInfo member,String opReason,
);
// void 	onReceiveJoinApplication (String groupID, V2TIMGroupMemberInfo member, String opReason)
typedef OnApplicationProcessedCallback = void Function(String groupID,V2TimGroupMemberInfo opUser,bool isAgreeJoin,String opReason,
);
// void 	onApplicationProcessed (String groupID, V2TIMGroupMemberInfo opUser, boolean isAgreeJoin, String opReason)
typedef OnGrantAdministratorCallback = void Function(String groupID,V2TimGroupMemberInfo opUser,List<V2TimGroupMemberInfo> memberList,
);
// void 	onGrantAdministrator (String groupID, V2TIMGroupMemberInfo opUser, List< V2TIMGroupMemberInfo > memberList)
typedef OnRevokeAdministratorCallback = void Function(String groupID,V2TimGroupMemberInfo opUser,List<V2TimGroupMemberInfo> memberList,
);
// void 	onRevokeAdministrator (String groupID, V2TIMGroupMemberInfo opUser, List< V2TIMGroupMemberInfo > memberList)
typedef OnQuitFromGroupCallback = void Function(String groupID,
);
// void 	onQuitFromGroup (String groupID)
typedef OnReceiveRESTCustomDataCallback = void Function(String groupID,String customData,
);
// void 	onReceiveRESTCustomData (String groupID, byte[] customData)
typedef OnGroupAttributeChangedCallback = void Function(String groupID,Map<String, String> groupAttributeMap,
);
// void 	onGroupAttributeChanged (String groupID, Map< String, String > groupAttributeMap)
typedef OnRecvNewMessageCallback = void Function(V2TimMessage msg,
);// void 	onGroupAttributeChanged (String groupID, Map< String, String > groupAttributeMap)
typedef OnRecvMessageModified = void Function(V2TimMessage msg,
);// void 	onRecvNewMessage (V2TIMMessage msg)
typedef OnRecvC2CReadReceiptCallback = void Function(List<V2TimMessageReceipt> receiptList,
);
// void 	onRecvC2CReadReceipt (List< V2TIMMessageReceipt > receiptList)
typedef OnRecvMessageRevokedCallback = void Function(String msgID,
);
// void 	onRecvMessageRevoked (String msgID)
//
typedef OnFriendApplicationListAddedCallback = void Function(List<V2TimFriendApplication> applicationList,
);
// void 	onFriendApplicationListAdded (List< V2TIMFriendApplication > applicationList)
typedef OnFriendApplicationListDeletedCallback = void Function(List<String> userIDList,
);
// void 	onFriendApplicationListDeleted (List< String > userIDList)
typedef OnFriendApplicationListReadCallback = void Function();
// void 	onFriendApplicationListRead ()
typedef OnFriendListAddedCallback = void Function(List<V2TimFriendInfo> users);
// void 	onFriendListAdded (List< V2TIMFriendInfo > users)
typedef OnFriendListDeletedCallback = void Function(List<String> userList);
// void 	onFriendListDeleted (List< String > userList)
typedef OnBlackListAddCallback = void Function(List<V2TimFriendInfo> infoList);
// void 	onBlackListAdd (List< V2TIMFriendInfo > infoList)
typedef OnBlackListDeletedCallback = void Function(List<String> userList);
// void 	onBlackListDeleted (List< String > userList)
typedef OnFriendInfoChangedCallback = void Function(List<V2TimFriendInfo> infoList,
);
// void 	onFriendInfoChanged (List< V2TIMFriendInfo > infoList)
typedef OnConversationChangedCallback = void Function(List<V2TimConversation> conversationList,
);
// void 	onNewConversation (List< V2TIMConversation > conversationList)
typedef OnNewConversation = void Function(List<V2TimConversation> conversationList,
);// void 	onConversationChanged (List< V2TIMConversation > conversationList)
typedef OnReceiveNewInvitationCallback = void Function(String inviteID,String inviter,String groupID,List<String> inviteeList,String data,
);
// void 	onReceiveNewInvitation (String inviteID, String inviter, String groupID, List< String > inviteeList, String data)
typedef OnInviteeAcceptedCallback = void Function(String inviteID,String invitee,String data,
);
// void 	onInviteeAccepted (String inviteID, String invitee, String data)
typedef OnInviteeRejectedCallback = void Function(String inviteID,String invitee,String data,
);
// void 	onInviteeRejected (String inviteID, String invitee, String data)
typedef OnInvitationCancelledCallback = void Function(String inviteID,String inviter,String data,
);
// void 	onInvitationCancelled (String inviteID, String inviter, String data)
typedef OnInvitationTimeoutCallback = void Function(String inviteID,List<String> inviteeList,
);
// void 	onInvitationTimeout (String inviteID, List< String > inviteeList)
//
typedef OnSendMessageProgressCallback = void Function(V2TimMessage message,int progress,
);
typedef OnRecvMessageReadReceipts = void Function(List<V2TimMessageReceipt> receiptList,
);
typedef OnConversationGroupCreated = void Function(String groupName, List<V2TimConversation> conversationList);typedef OnConversationGroupDeleted = void Function(String groupName);typedef OnConversationGroupNameChanged = void Function(String oldName, String newName);typedef OnConversationsAddedToGroup = void Function(String groupName, List<V2TimConversation> conversationList);typedef OnConversationsDeletedFromGroup = void Function(String groupName, List<V2TimConversation> conversationList);typedef OnTopicCreated = void Function(String groupID, String topicID);typedef OnTopicDeleted = void Function(String groupID, List<String> topicIDList);typedef OnTopicInfoChanged = void Function(String groupID, V2TimTopicInfo topicInfo);typedef OnGroupCounterChanged = void Function(String groupID,String key,int newValue,
);

 typedef 函数 实践:

按照使用的场景,最多出现的地方就是我们的 回调函数,不管是按钮的,还是其他的地方,和其他语言的回调函数类似:

  • javascript    Function,  箭头函数
  • c#  事件和委托

都是可以 把函数 当做参数传递,然后在另外一个地方等待调用

typedef 函数 高阶用法示例:

► 腾讯 im中的 使用 typedef 实例

  @overrideFuture<bool?> init({/// Callback from TUIKit invoke, includes IM SDK API error, notify information, Flutter error.ValueChanged<TIMCallback>? onTUIKitCallbackListener,required int sdkAppID,required LogLevelEnum loglevel,required V2TimSDKListener listener,LanguageEnum? language,String? extraLanguage,TIMUIKitConfig? config,/// Specify the current device platform, mobile or desktop, based on your needs./// TUIKit will automatically determine the platform if no specification is provided. DeviceType? platform,DeviceType? platform,VoidCallback? onWebLoginSuccess}) async {省略
}
 V2TimSDKListener  listener  定义如下  
class V2TimSDKListener {VoidCallback onConnecting = () {};VoidCallback onConnectSuccess = () {};ErrorCallback onConnectFailed = (int code, String error) {};VoidCallback onKickedOffline = () {};VoidCallback onUserSigExpired = () {};V2TimUserFullInfoCallback onSelfInfoUpdated = (V2TimUserFullInfo info,) {};OnUserStatusChanged onUserStatusChanged = (List<V2TimUserStatus> userStatusList){};OnLog onLog = (int logLevel,String logContent){};V2TimSDKListener({ErrorCallback? onConnectFailed,VoidCallback? onConnectSuccess,VoidCallback? onConnecting,VoidCallback? onKickedOffline,V2TimUserFullInfoCallback? onSelfInfoUpdated,VoidCallback? onUserSigExpired,OnUserStatusChanged? onUserStatusChanged,OnLog? onLog,}) {if (onConnectFailed != null) {this.onConnectFailed = onConnectFailed;}if (onConnectSuccess != null) {this.onConnectSuccess = onConnectSuccess;}if (onConnecting != null) {this.onConnecting = onConnecting;}if (onKickedOffline != null) {this.onKickedOffline = onKickedOffline;}if (onSelfInfoUpdated != null) {this.onSelfInfoUpdated = onSelfInfoUpdated;}if (onUserSigExpired != null) {this.onUserSigExpired = onUserSigExpired;}if(onUserStatusChanged!=null){this.onUserStatusChanged = onUserStatusChanged;}if(onLog!=null){this.onLog = onLog;}}
}

他的嵌套使用很普遍,甚至直接 定义一个类型来接收 回调函数:合理使用的情况下,会给我们代码提供很大便捷

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/679245.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

解释Java中的并发集合类,比如ConcurrentHashMap和CopyOnWriteArrayList

解释Java中的并发集合类&#xff0c;比如ConcurrentHashMap和CopyOnWriteArrayList 在Java中&#xff0c;有许多用于并发编程的集合类&#xff0c;它们提供了线程安全的操作&#xff0c;可以在多线程环境中安全地访问和修改数据。两个常见的并发集合类是 ConcurrentHashMap 和…

【CTFshow】VIP题目限免 通关

&#x1f36c; 博主介绍&#x1f468;‍&#x1f393; 博主介绍&#xff1a;大家好&#xff0c;我是 hacker-routing &#xff0c;很高兴认识大家~ ✨主攻领域&#xff1a;【渗透领域】【应急响应】 【python】 【VulnHub靶场复现】【面试分析】 &#x1f389;点赞➕评论➕收藏…

我主编的电子技术实验手册(03)——电阻的识别与测量

本专栏是笔者主编教材&#xff08;图0所示&#xff09;的电子版&#xff0c;依托简易的元器件和仪表安排了30多个实验&#xff0c;主要面向经费不太充足的中高职院校。每个实验都安排了必不可少的【预习知识】&#xff0c;精心设计的【实验步骤】&#xff0c;全面丰富的【思考习…

AI大模型学习笔记之四:生成式人工智能(AIGC)是如何工作的?

OpenAI 发布 ChatGPT 已经1年多了&#xff0c;生成式人工智能&#xff08;AIGC&#xff09;也已经广为人知&#xff0c;我们常常津津乐道于 ChatGPT 和 Claude 这样的人工智能系统能够神奇地生成文本与我们对话&#xff0c;并且能够记忆上下文情境。 Midjunery和DALLE 这样的AI…

随机过程及应用学习笔记(一)概率论(概要)

概率是随机的基础&#xff0c;在【概率论&#xff08;概要&#xff09;】这个部分中仅记录学习随机过程及应用的基本定义和结果。 前言 首先&#xff0c;概率论研究的基础是概率空间。概率空间由一个样本空间和一个概率测度组成&#xff0c;样本空间包含了所有可能的结果&…

90.Go语言中实现可选参数的几种方法:可变长参数、使用Map、结构体和函数选项模式

文章目录 导言方法1&#xff1a;可变长参数&#xff08;Variadic Args&#xff09;方法2&#xff1a;使用Map方法3&#xff1a;使用结构体&#xff08;Structs&#xff09;方法4&#xff1a;函数选项模式&#xff08;Functional Options Pattern&#xff09;五、总结 导言 我们…

服务器解析漏洞及任意文件下载

1.服务器文件解析漏洞 文件解析漏洞,是指Web容器&#xff08;Apache、nginx、iis等&#xff09;在解析文件时出现了漏洞,以其他格式执行出脚本格式的效果。从而,黑客可以利用该漏洞实现非法文件的解析。 &#xff08;1) Apache linux系统中的apache的php配置文件在/etc/apac…

【数据结构】顺序栈和链式栈的简单实现和解析(C语言版)

数据结构——栈的简单解析和实现 一、概念二、入栈&#xff08;push&#xff09;三、出栈&#xff08;pop&#xff09;四、顺序栈简单实现 &#xff08;1&#xff09;进栈操作&#xff08;2&#xff09;出栈操作 一、概念 本篇所讲解的栈和队列属于逻辑结构上的划分。逻辑结构…

综合项目---博客

一.运行环境 192.168.32.132 Server-Web linux Web 192.168.32.133 Server-NFS-DNS linux NFS/DNS 基础配置 1.配置主机名静态ip 2.开启防火墙并配置 3.部分开启selinux并配置 4.服务器之间通过阿里云进行时间同步 5.服务器之间实现ssh免密…

SpringCloud-Ribbon:负载均衡(基于客户端)

6. Ribbon&#xff1a;负载均衡(基于客户端) 6.1 负载均衡以及Ribbon Ribbon是什么&#xff1f; Spring Cloud Ribbon 是基于Netflix Ribbon 实现的一套客户端负载均衡的工具。简单的说&#xff0c;Ribbon 是 Netflix 发布的开源项目&#xff0c;主要功能是提供客户端的软件负…

Flask基础学习2

连接mysql数据库测试(专业版) [注意1&#xff1a;要导入text库&#xff0c;否则可能出现找不到select 1错误] [注意2&#xff1a;若出现下列问题&#xff0c;可按照模板代码的顺序db SQLAlchemy(app) 的位置] RuntimeError: Either SQLALCHEMY_DATABASE_URI or SQLALCHEMY_B…

MMKV:轻巧高效的跨平台键值存储解决方案

MMKV&#xff1a;轻巧高效的跨平台键值存储解决方案 引言 在移动应用的开发中&#xff0c;数据存储是一个至关重要的环节。随着移动应用的普及和功能的增多&#xff0c;应用需要存储和管理各种类型的数据&#xff0c;包括用户配置信息、缓存数据、临时状态等。传统的数据存储…

acwing14期周赛---------安排时间(贪心+枚举)

贝茜独立经营着一家餐厅&#xff0c;她一天的营业时间可以分为 n 个时段&#xff0c;编号 1∼n。 在这一天的营业中&#xff0c;她一共接收到了 m 个客人的预约用餐订单&#xff0c;编号 1∼m。 其中&#xff0c;第 i 个订单的相关信息如下&#xff1a; 贝茜在第 si个时段接到该…

python巧用定理判断素数

目录 判断一个数n是否是素数 求一个数的素因数个数 求大于等于指定数的最小素数 在数论中有三个非常重要的关于素数的定理 1、任何数都可以表示成若干个素数的乘积 2、任意数的一个素因子如果小于根号n&#xff0c;那么另一个与其对应的素因子必然大于根号n。 3、除了2和…

求小数的某一位(c++题解)

题目描述 分数化为小数后&#xff0c;小数点后第位的数字是多少&#xff1f; 输入格式 三个正整数&#xff0c;相邻两个数之间用单个空格隔开。 输出格式 一个数字。 样例 输入样例 复制1 2 1输出样例 复制5 ______________________________________________________…

pytorch张量和numpy数组相互转换

pytorch张量和numpy数组相互转换 &#x1f335;文章目录&#x1f335; &#x1f333;引言&#x1f333;&#x1f333;将numpy数组转换为Pytorch张量&#x1f333;1. 功能介绍2. 用法 &#x1f333;将Pytorch张量转换为numpy数组&#x1f333;1. 功能介绍2. 用法 &#x1f333;P…

使用python写一个二叉树

可以使用Python的类来实现二叉树&#xff0c;每个节点包括一个值和指向左右子节点的引用。 class Node:def __init__(self, value):self.value valueself.left Noneself.right Noneclass BinaryTree:def __init__(self, root):self.root Node(root)def insert(self, value…

浅谈应该遵守的伦敦银交易规则

做伦敦银投资的朋友应遵守伦敦银交易规则&#xff0c;伦敦银交易规则不是指那些伦敦银交易技巧&#xff0c;而是在这个市场中要遵循的一些约定&#xff0c;下面我们就来讨论一下。 风险管理。风险管理即指投资者控制自己一笔乃至整体交易的风险&#xff0c;没有风险管理意识的投…

JavaSE——方法(1/2)-介绍、方法的各种形式、使用的要求

目录 方法的介绍 方法的其他形式 方法使用的要求 方法的介绍 方法是什么 方法是一种语法结构&#xff0c;它可以把一段代码封装成一个功能&#xff0c;以便重复调用。 方法的完整格式 修饰符 返回值类型 方法名 ( 形参列表 ) { 方法体代码(需要执行的功能代码) return…

OpenCV-36 多边形逼近与凸包

目录 一、多边形的逼近 二、凸包 一、多边形的逼近 findContours后的轮廓信息countours可能过于复杂不平滑&#xff0c;可以用approxPolyDP函数对该多边形曲线做适当近似&#xff0c;这就是轮廓的多边形逼近。 apporxPolyDP就是以多边形去逼近轮廓&#xff0c;采用的是Doug…