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. Spark2.x(五十五):在spark structured streaming下sink file(parquet,csv等),正常运行一段时间后:清理掉checkpoint,重新启动app,无法sink记录(file)到hdfs。

    场景: 在spark structured streaming读取kafka上的topic,然后将统计结果写入到hdfs,hdfs保存目录按照month,day,hour进行分区: 1)程序放到spa ...

  2. CentOS 6和 CentOS 7的区别【转】

    虽然,redhat 8在今年已经推出了,但是centos 8还没有推出.而且公司好多都在用centos 6和7 来了解一下6和7的区别吧 整体说明 1.系统 项目CentOS 6CentOS7 . 安 ...

  3. mysql查询列定义,是否自增等

    SELECT ORDINAL_POSITION AS Colorder, Column_Name AS ColumnName, data_type AS TypeName, COLUMN_COMMEN ...

  4. PhpStorm使用sftp实现代码自动上传服务器

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/huihui940630/article/ ...

  5. redis 使用redis Desktop manger进行远程进行链接

    1.修改redis.conf文件: a.去掉bind:127.0.0.0 b.protected mode 模式改成 no 2.重启redis /etc/init.d/redis restart 3. ...

  6. activiti 自定义用户

    https://blog.csdn.net/meng564764406/article/details/53789958 此文目的: 对网络上的关于对activiti 使用做一个总结,因为很难找到一个 ...

  7. Win10 LTSC 2019 长期支持版

    win 10 LTSB 2016 文件名:cn_windows_10_enterprise_2016_ltsb_x86_dvd_9057089.iso (2.62GB) 语言: Chinese – S ...

  8. Spring cloud微服务安全实战-6-10sentinel之热点和系统规则

    热点规则 热点就是经常访问的数据.很多时候我们希望争对某一些热点数据,然后来进行限制.比如说商品的信息这个服务,我们给它做一个限流,qps是100,某一天我想做一个秒杀活动,可能会有很大的流量,这个时 ...

  9. Centos7.3使用脚本自动静默安装oracle11.2.0.4数据库

    一直想着写一个脚本实现自动化安装oracle数据库.以下内容实验过几次了,可能还存在些小问题,如果在跑以下脚本中遇到问题,自己仔细排查即可 挣扎了好久,总算还是没实现,目前只能通过依次执行多个脚本来安 ...

  10. [LeetCode] 276. Paint Fence 粉刷篱笆

    There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...