zoukankan      html  css  js  c++  java
  • isolate 通信

    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++;
      }
    }
    

      

  • 相关阅读:
    学习笔记:@RequestMapping
    学习笔记:serializable接口实现Java对象序列化
    学习笔记:JWT在spring中的时间
    学习笔记:JWT学习
    《OpenGL编程指南》学习进度备忘
    【练习7.4】使用直方图陆地移动距离EMD区分不同光线条件下的图片cvCalcEMD2
    【练习7.3】从直方图创建signature、计算两个直方图的EMD距离
    自动变量和开辟内存的生存期和作用域探讨
    【练习7.2】直方图归一化cvNormalizeHist、匹配cvCompareHist及各种匹配方法
    【练习7.1】cvCreateHist创建直方图、cvCalcHist计算直方图、cvQueryHistValue_1D访问直方图及右左法则
  • 原文地址:https://www.cnblogs.com/pythonClub/p/10822365.html
Copyright © 2011-2022 走看看