isolate sqflite demo

main.dart

import 'package:flutter/material.dart';
import 'demo_isolates.dart';
import 'package:rxdart/rxdart.dart';
import 'dbhelper.dart';
import 'package:flutter_isolate/flutter_isolate.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      initialRoute: '/',
      routes: {
        '/':(context)=>MyHome(),
        '/second':(context)=>SecondPage(),
      },
    );
  }
}

class MyHome extends StatelessWidget{
  DataBloc dataBloc = DataBloc();
  FlutterIsolate isolate;
  DB db = DB();

  checkData()async{
    var a = await db.queryData('SELECT * FROM type');
    print(a);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('sss'),),
      body: Container(child: Column(
        children: <Widget>[
          StreamBuilder(
            stream: dataBloc.dataBloc.stream,
            builder: (context, snapshot){
              if(snapshot.hasData){
                return Text('${snapshot.data}');
              }else{
                return Text('pending');
              }
            },
          ),
          RaisedButton(child: Text('start'),onPressed: ()async{
            isolate = await spawnIsolate('abc', dataBloc);
          },),
          RaisedButton(child: Text('stop'),onPressed: (){
            isolate.kill();
          },),
          RaisedButton(child: Text('check'),onPressed: (){
            checkData();
          },),
          RaisedButton(child: Text('go to 2'),onPressed: (){
            Navigator.of(context).pushNamed('/second');
          },),
        ],
      ),),
    );
  }
}

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('second page'),),
      body: Container(child: Center(child: Text('second'),),),
    );
  }
}

class DataBloc {
  ReplaySubject dataBloc = ReplaySubject();
}

  

 

demo_isolates.dart

import 'dart:async';
import 'dart:isolate';
import 'dbhelper.dart';
import 'main.dart';
import 'package:flutter_isolate/flutter_isolate.dart';

Future<FlutterIsolate> spawnIsolate(String type, DataBloc bloc) async {
  ReceivePort receivePort = new ReceivePort();
  ReceivePort listenPort = ReceivePort();
  FlutterIsolate isolate = await FlutterIsolate.spawn(dartIsolateLongRunningOperation, receivePort.sendPort);
  SendPort t = await receivePort.first;
  t.send({'sender':listenPort.sendPort, 'msg':type});

  listenPort.listen((data){
    bloc.dataBloc.add(data);
  });
  return isolate;
}

void dartIsolateLongRunningOperation(SendPort sendPort) async {
  ReceivePort port = ReceivePort();
  sendPort.send(port.sendPort);
  port.listen((data)async{
    await doWork(data['msg'], data['sender']);
  });
}

Future doWork(String type, SendPort sender)async{
  DB db = DB();
  int i = 0;
  while(i<30){
    var a = await db.addData('type', {'name':'$i'});
    sender.send('id: $a');
    i++;
    await Future.delayed(Duration(seconds: 1));
  }

}

  

posted @ 2019-05-06 22:24  CrossPython  阅读(493)  评论(0编辑  收藏  举报