博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
isolate 通信
阅读量:5317 次
发布时间:2019-06-14

本文共 3511 字,大约阅读时间需要 11 分钟。

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
createState() { return MyAppState(); }}class MyAppState extends State
{ 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:
[ 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
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++; }}

  

转载于:https://www.cnblogs.com/pythonClub/p/10822365.html

你可能感兴趣的文章
原生JavaScript第六篇
查看>>
安装Endnote X6,但Word插件显示的总是Endnote Web"解决办法
查看>>
python全栈 计算机硬件管理 —— 硬件
查看>>
大数据学习
查看>>
简单工厂模式
查看>>
Delphi7编译的程序自动中Win32.Induc.a病毒的解决办法
查看>>
Objective-C 【关于导入类(@class 和 #import的区别)】
查看>>
倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-点击运行按钮进入到运行状态报错Error starting TwinCAT System怎么办 AdsWarning1823怎么办...
查看>>
【转】javascript 中的很多有用的东西
查看>>
Centos7.2正常启动关闭CDH5.16.1
查看>>
Android 监听返回键、HOME键
查看>>
Android ContentProvider的实现
查看>>
sqlserver 各种判断是否存在(表名、函数、存储过程等)
查看>>
给C#学习者的建议 - CLR Via C# 读后感
查看>>
Recover Binary Search Tree
查看>>
Java 实践:生产者与消费者
查看>>
[转]IOCP--Socket IO模型终结篇
查看>>
(五)归一化
查看>>
使用信号量
查看>>
《数据分析实战》--第三章 python实现
查看>>