isolate 通信
main.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 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 + + ; } } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步