当前位置:网站首页>Flutter 2进阶(六):回调函数的使用
Flutter 2进阶(六):回调函数的使用
2022-07-22 10:21:00 【吴庆森】
我看了一下,关于 Flutter 的回调和 Android 中的有点区别。所以记录一下,
下面是定义了一个有两个参数的回调:
//定义回调
typedef MyCallBack = Function(String val1, String val2);
//回调监听
class MyCallBackListener {
final MyCallBack myCallBack;
MyCallBackListener({this.myCallBack});
}
设置我收到的回调:
MyCallBackListener(myCallBack: (String val1, String val2) {
print('收到回调:' + 'val1:' + val1 + 'val2:' + val2);
}
没参数的回调可以将上面的 MyCallBack 替换为系统提供的 VoidCallback;也可以直接把上面两个参数去掉;
定义回调并设置回调的完整代码:
import 'package:data_plugin/bmob/response/bmob_error.dart';
import 'package:data_plugin/bmob/response/bmob_saved.dart';
import 'package:data_plugin/bmob/table/bmob_object.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_blbl/utils/toast.dart';
import 'home_page_bean.dart';
class DataBaseUtil {
///保存一条数据
static saveSingle(BmobObject bmobObject,
{bool isShowToast, MyCallBackListener callBackListener}) {
bmobObject.save().then((BmobSaved bmobSaved) {
if (isShowToast) {
showToast('数据保存成功');
//设置回调
callBackListener.myCallBack("参数1", "参数2");
}
}).catchError((e) {
if (isShowToast) {
showToast('数据保存失败:' + BmobError.convert(e).error);
//设置回调
callBackListener.myCallBack("参数1", "参数2");
}
});
}
}
//定义回调
typedef MyCallBack = Function(String val1, String val2);
//回调监听
class MyCallBackListener {
final MyCallBack myCallBack;
MyCallBackListener({this.myCallBack});
}
设置回调并收到回调:
_saveHomeDb() {
HomePageBean homePage = HomePageBean();
homePage.title = "电影剪辑";
homePage.classname = "影视";
homePage.videourl = "https://v-cdn.zjol.com.cn/280443.mp4";
homePage.videoLength = "00:36";
homePage.playnum = '1.4万';
homePage.content = '精彩剪辑内容';
homePage.type = 1;
DataBaseUtil.saveSingle(homePage, isShowToast: true, callBackListener:
MyCallBackListener(myCallBack: (String val1, String val2) {
print('收到回调:' + 'val1:' + val1 + 'val2:' + val2);
}));
}
上面的 saveSingle 方法中参数有 {} 修饰表示参数可以传也可以不传。
边栏推荐
- jvm-jhat(虚拟机堆转储快照分析工具)
- Pyinstaller packaging scene
- Oracle 11g 基于CentOS7安装并启动em
- Mysql之一主多从复制
- leetcode 21. 合并两个有序链表
- Data Lake (18): Flink and iceberg integrate SQL API operations
- promise用法
- Oracle 11g installs and starts EM based on centos7
- Persistence of spark RDD (cache, checkpoints, broadcast variables, and accumulators)
- Creation of sparksql dataset
猜你喜欢
How Oracle converts strings into multiple lines
Pregel function in spark graphx (Reprint)
Data model design of newsql database
Data architecture and database modeling
Common performance tools: if you want to be good at something, you must first sharpen its tools
JVM-VisualVM:多合-故障处理工具
linux开启MySQL binlog日志
Datablau5.0数据资产管理产品系列重磅发布
IO models you often encounter
From data standards to database design: solve the last mile problem of basic data standards (Part 2)
随机推荐
mysql grant 用户权限总结
Spark SQL 内置函数和自定义函数UDF
MySQL之插入、修改、删除表数据
撞库、脱库和洗库
getchar的使用
MYSQL5.7的安装(yum、二进制、编译安装)
IDEA运行WordCount程序(详细步骤)
Decimal operation in shell (BC)
Data model design of newsql database
From data standards to database design: solve the last mile problem of basic data standards (Part 1)
Conception du modèle de données de la base de données newsql
Modify the contents of /etc/crontab file directly, and the scheduled task will not take effect
Spark SQL built-in functions and custom functions UDF
记一次优化我的个人博客
NiO character set and charset
It is found that the MariaDB database is 12 hours late, and the xxljob scheduled task scheduling is abnormal
Pure function and higher order function
Data Lake (18): Flink and iceberg integrate SQL API operations
The luckiest geek in China: in his early twenties, he was worth more than 100 million, and retired after three startups
数组push时 覆盖的问题