isolate 通信

Posted pythonclub

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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++;
  }
}

  

以上是关于isolate 通信的主要内容,如果未能解决你的问题,请参考以下文章

与另一个片段通信的片段接口

无法通过接口获取与片段通信的活动

如何在嵌套片段内的两个子片段之间进行通信

与 viewpager 内的片段的父片段通信

片段通信问题(尝试调用虚方法)

片段和服务器之间的通信问题