Isolate--------------------------------------------------------
void main() {
runApp(MyApp());
try {
/**
* ReceivePort
*/
SendPort childSendPort;
ReceivePort receivePort = new ReceivePort();
Isolate.spawn(isolateMain, receivePort.sendPort);
receivePort.listen((message) {
if (message is SendPort) {
print("这里收到 子线程 的发送器啦!");
message.send(message);
print("发送 主线程发送器");
childSendPort = message;
} else {
print("这里收到 子线程 的消息啦!");
if (childSendPort != null) {
print("发送主线程消息");
childSendPort.send("主线程消息");
}
}
});
} catch (e) {
print(e); //输出-->异常:分母不可以为0
}
}
void isolateMain(SendPort sendPort) {
ReceivePort receivePort = new ReceivePort();
sendPort.send(receivePort.sendPort);
print("发送 子线程发送器");
sendPort.send("子线程消息");
print("发送子线程消息");
receivePort.listen((message) {
if (message is SendPort) {
print("这里收到 主线程 的消息啦!${message}");
} else {
print("这里收到 主线程 的消息啦!${message}");
}
});
}
/**
*I/flutter: 发送 子线程发送器
*I/flutter: 发送子线程消息
*I/flutter: 发送 主线程发送器
*I/flutter: 这里收到 子线程 的发送器啦!
*I/flutter: 这里收到 子线程 的消息啦!
*I/flutter: 发送主线程消息
*I/flutter: 这里收到 主线程 的消息啦!SendPort
*I/flutter: 这里收到 主线程 的消息啦!主线程消息
*/
执行优先-------------------------------------------------------
/**
* 执行延时
*/
Future.delayed(new Duration(seconds: 5), (){
getUserInfo("22");
saveUserInfo("delayed running");
}).then((value){
print("then running = ${value}");
}).catchError((error){
print("catch error = ${error}");
});
/**
* 执行优先
*/
Future.microtask((){
login("jeck", "998899");
});
/**
* 与构造函数同步执行
*/
Future.sync((){
print("sync running");
});
//I/flutter: sync running
//I/flutter: user = jeck + 998899
//I/flutter: id = 22
//I/flutter: userinfo = delayed running
//I/flutter: then running = null
异步执行-------------------------------------------------------
/**
* 异步执行
*/
Stream.fromFutures([loginFuture, userInfoFuture, saveUserInfoFuture])
.listen((data) {
print("各个任务返回的结果回调${data}");
},onError: ((err) {
print("各个任务执行失败的回调${err}");
}), onDone: ((){
print("监听各个任务执行过程中完成回调");
})).onDone((){
print("所有任务全部完成的回调");
});
Future<String> loginFuture = Future<String>((){
login("name", "999");
});
String login(String name, String psw){
print("user = ${name} + ${psw}");
}
Future<String> userInfoFuture = Future<String>((){
getUserInfo("123");
});
String getUserInfo(String id){
print("id = ${id}");
}
Future<String> saveUserInfoFuture = Future<String>((){
saveUserInfo("user lisa");
});
String saveUserInfo(String info){
print("userinfo = ${info}");
}
//I/flutter: user = name + 999
//I/flutter: 各个任务返回的结果回调null
//I/flutter: id = 123
//I/flutter: 各个任务返回的结果回调null
//I/flutter: userinfo = user lisa
//I/flutter: 各个任务返回的结果回调null
//I/flutter: 所有任务全部完成的回调
流传输------------------------------------------------------
/**
* 创建流
*/
StreamController controller = StreamController();
StreamSubscription subscription = controller.stream.listen((data) {
Student getData = data as Student;
String name = getData.name;
int age = getData.age;
print("name = ${name}, age = ${age}");
});
Student student = new Student("name", 22);
controller.sink.add(student);
//I/flutter: name = name, age = 22
/**
* 流转换 接收到数据后,进行处理再传给控制器
*/
StreamController<Student> controller = StreamController<Student>();
var transformer = StreamTransformer<Student, String>.fromHandlers(
handleData: (value, sink) {
if(value == 1) {
sink.add("操作正确");
} else {
sink.addError("操作失败,再试一次吧");
}
});
/**
* 创建流 通过transform(transformer) 将流转换
*/
controller.stream.transform(transformer).listen((data) {
print(
"controller data = ${data}");
},
onError: (err) {
print(
"controller err = ${err}");
});
controller.sink.add(Student("jack", 23));
//controller err = 操作失败,再试一次吧
网友评论