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. Flume(一) —— 启动与基本使用

    基础架构 Flume is a distributed, reliable(可靠地), and available service for efficiently(高效地) collecting, a ...

  2. spring-JDBC模板

    Spring的JDBC的模板 Spring是EE开发的一站式的框架,有EE开发的每层的解决方案. Spring对持久层也提供了解决方案:ORM模块和JDBC的模板. Spring提供了很多的模板用于简 ...

  3. 第06组 Alpha冲刺(4/6)

    队名:拾光组 组长博客链接 作业博客链接 团队项目情况 燃尽图(组内共享) 组长:宋奕 过去两天完成了哪些任务 主要完成了用户联系模块的接口设计 完善后端的信息处理 GitHub签入记录 接下来的计划 ...

  4. 剑指offer:扑克牌顺子

    题目描述: LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他 ...

  5. Android HIDL学习(2) ---- HelloWorld【转】

    本文转载自: 写在前面 程序员有个癖好,无论是学习什么新知识,都喜欢以HelloWorld作为一个简单的例子来开头,咱们也不例外. OK,咱这里都是干货,废话就不多说啦,学习HIDL呢咱们还是需要一些 ...

  6. Protobuf多协议

    上一篇只有Person的message,如果多了一个message,如Dog,这样就会有问题. 解决方法: 定义多协议 一.定义proto文件 syntax = "proto2"; ...

  7. svn忽略obj

    于是换成第二种方式 Properties => News => Other => svn:ignore 将你要过滤的文件夹放入文本框里面,此处因为要过滤的是bin和obj所以各占一行 ...

  8. [转]eclipse中explorer显示方式

    原文地址:https://www.cnblogs.com/gne-hwz/p/7590451.html 不知道是不是上面的描述.做个记录 project explorer 项目资源管理器 这个要打开代 ...

  9. openresty开发系列34--openresty执行流程之4访问阶段

    openresty开发系列34--openresty执行流程之4访问阶段 访问阶段 用途:访问权限限制 返回403 nginx:allow 允许,deny 禁止 allow ip:deny ip: 涉 ...

  10. 联合主键SQL 联合索引

    联合主键索引 联合索引 ALTER TABLE [dbo].[Tb_Shop] ADD CONSTRAINT [shopno] UNIQUE NONCLUSTERED ( [ShopGid] ASC, ...