main.dart

import 'package:flutter/material.dart';
import 'package:flutter_isolate/flutter_isolate.dart';
import 'isolates.dart';
import 'dbhelper.dart';
import 'package:rxdart/rxdart.dart';
import 'dart:isolate'; void main() {
runApp(MaterialApp(
title: 'Flutter Demo',
initialRoute: '/',
routes: {
'/':(context)=>MyApp(),
'/second':(context)=>NextPage(),
},
));
} class MyApp extends StatefulWidget{
@override
State<StatefulWidget> createState() {
return MyAppState();
}
}
class MyAppState extends State<MyApp> {
FlutterIsolate isoltex;
DataBloc bloc = DataBloc();
ReceivePort toChild;
DB db = DB(); addData()async{
var a = await db.addData('type', {'name':'11maintest'});
print(a);
} checkData()async{
var b = await db.queryData('SELECT * FROM type');
print(b);
}
@override
Widget build(BuildContext context) { return Scaffold(
appBar: AppBar(title: Text('sss'),),
body: Container(child: Column(
children: <Widget>[
StreamBuilder(
stream: bloc.dataBloc.stream,
builder: (context, snapshot){
if(snapshot.hasData){
return Text('${snapshot.data}');
}else{
return Text('pending..');
}
},
),
RaisedButton(child: Text('addData'),onPressed: ()async{
addData();
},),
RaisedButton(child: Text('check data'),onPressed: ()async{
checkData();
},),
RaisedButton(child: Text('start'),onPressed: ()async{
isoltex = await createIsolate(bloc,'abc');
},),
RaisedButton(child: Text('pause'),onPressed: (){
isoltex.pause();
},),
RaisedButton(child: Text('resume'),onPressed: (){
isoltex.resume();
},),
RaisedButton(child: Text('kill'),onPressed: (){
isoltex.kill();
},),
RaisedButton(child: Text('go to next'),onPressed: (){
Navigator.of(context).pushNamed('/second');
},) ]),
),
);
}
} class NextPage extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('page2'),),
body: Container(
child: RaisedButton(child:Text('btn'),
onPressed: (){
print('hello');
}),
),
);
}
} class DataBloc {
ReplaySubject dataBloc = ReplaySubject(); }

  

isolates.dart

import 'package:flutter_isolate/flutter_isolate.dart';
import 'dart:async';
import 'dart:isolate';
import 'main.dart'; Future<FlutterIsolate> createIsolate(DataBloc bloc, String type) async {
ReceivePort receivePort = ReceivePort();
ReceivePort fromChild = ReceivePort();
FlutterIsolate isolate = await FlutterIsolate.spawn(isolateEntry, receivePort.sendPort);
SendPort t = await receivePort.first;
t.send({'sender':fromChild.sendPort,'msg':type});
fromChild.listen((value){
bloc.dataBloc.add(value);
});
return isolate;
} isolateEntry(SendPort sendPort)async{
ReceivePort port = ReceivePort();
sendPort.send(port.sendPort); port.listen((data)async{
print('son $data');
await doWork(data['msg'], data['sender']);
});
} Future doWork(data, SendPort s)async{
print('start working');
int i = 0;
while(i<10){
s.send('$data : $i');
await Future.delayed(Duration(seconds: 1));
i++;
}
}

  

isolate 通信的更多相关文章

  1. 关于nodeJS多线程的支持,目前看来无法实现,讲解v8的一些东西

    关于这个,我这几天一直在研究,国内关于v8的资料很少,stackoverflow上也不多. 说起来我得说声抱歉,虽然并没有承诺什么.这个功能大概是无法实现.下面我来解释一下为什么. 首先我们要了解一下 ...

  2. Go断后,Dart冲前,Google的野心

    今天,我要讲讲新的前端web语言-Dart 我是网上看到这段话,激起了兴趣,虽然我不能算是真正的web前端从业者!哈哈 [ Dart语言由谷歌制造,用来替代Javascript,弥补Javascrip ...

  3. Flutter--Dart基础语法(四)异步

    前言 Flutter 是 Google 开源的 UI 工具包,帮助开发者通过一套代码库高效构建多平台精美应用,Flutter 开源.免费,拥有宽松的开源协议,支持移动.Web.桌面和嵌入式平台. Fl ...

  4. AngularJS 全局scope与Isolate scope通信

    在项目开发时,全局scope 和 directive本地scope使用范围不够清晰,全局scope与directive本地scope通信掌握的不够透彻,这里对全局scope 和 directive本地 ...

  5. 全局scope与Isolate scope通信

    AngularJS 全局scope与Isolate scope通信 在项目开发时,全局scope 和 directive本地scope使用范围不够清晰,全局scope与directive本地scope ...

  6. AngularJS 全局scope与指令 scope通信

    在项目开发时,全局scope 和 directive本地scope使用范围不够清晰,全局scope与directive本地scope通信掌握的不够透彻,这里对全局scope 和 directive本地 ...

  7. angularjs学习之八(angularjs中isolate scope的使用)

    angular js中指令directive有个特别实用的东西,那就是 isolate scope (被隔离的scope) 关于详细他和全局的scope 有什么差别.能够參考以下这篇博文: Angul ...

  8. isolate两三事

    1.1. 第一步:创建并握手 如前所述,Isolate 不共享任何内存并通过消息进行交互,因此,我们需要找到一种方法在「调用者」与新的 isolate 之间建立通信. 每个 Isolate 都暴露了一 ...

  9. 理解加密算法(三)——创建CA机构,签发证书并开始TLS通信

    接理解加密算法(一)--加密算法分类.理解加密算法(二)--TLS/SSL 1 不安全的TCP通信 普通的TCP通信数据是明文传输的,所以存在数据泄露和被篡改的风险,我们可以写一段测试代码试验一下. ...

随机推荐

  1. 单细胞数据normalization方法 | SCTransform

    SCTransform Normalization and variance stabilization of single-cell RNA-seq data using regularized n ...

  2. Mysql创建测试大量测试数据

    修改mysql配置 max_heap_table_size=4000M innodb_flush_log_at_trx_commit=0sync_binlog=500 创建测试数据库 create d ...

  3. USB安装ESXi出错,menu.c32 not a com32r image

    USB安装EXSi出错,menu.c32 not a com32r image 不能进入安装界面. 提供提取的menu.c32 下载下来覆盖U盘根目录源文件 EXSi6.7测试可以用 文件csdn下载 ...

  4. window上可以执行的shell脚本,复制到linux上执行报错了

    原因:.sh脚本在windows系统下用记事本文件编写的.不同系统的编码格式引起的. 解决方法:修改.sh文件格式 (1)使用vi工具vi build_android.sh (2)利用如下命令查看文件 ...

  5. Win10 cmd控制台程序会被鼠标单击暂停的解决办法

    右键单击顶部白框,选择属性或默认值,将快速编辑模式的勾取消就可以了,最后记得点击确定

  6. Oracle系列五 多表查询

    笛卡尔集笛卡尔集会在下面条件下产生: 省略连接条件 连接条件无效 所有表中的所有行互相连接 为了避免笛卡尔集, 可以在 WHERE 加入有效的连接条件. Oracle 连接 使用连接在多个表中查询数据 ...

  7. vue的mixin简化开发

    vue的mixin可以将多个组件公用的声明周期方法和数据封装成一个对象,在不同的组件中自由插拔.实际做项目的时候,可以定义一些mixin,供多个组件使用.也非常有必要定义一个全局的mixin对象,对所 ...

  8. 【Js】单页面多个倒计时问题

    代码: <!DOCTYPE html> <html> <head> <title>多个timeout</title> <script ...

  9. Linux中搜索大于200M的文件

    Linux中清理磁盘空间时,经常需要找出大于200M的文件. 这个命令可以实现这个功能: find / -size +200M -exec du -h {} \;

  10. python脚本容器化

    https://blog.csdn.net/zstack_org/article/details/53099919 如何选择 base image: https://blog.csdn.net/nkl ...