zoukankan      html  css  js  c++  java
  • flutter isolate demo

    main.dart

    import 'package:flutter/material.dart';
    import 'package:flutter_isolate/flutter_isolate.dart';
    import 'isolates.dart';
    import 'dbhelper.dart';
    
    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;
      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>[
                RaisedButton(child: Text('addData'),onPressed: ()async{
                  addData();
                },),
                RaisedButton(child: Text('check data'),onPressed: ()async{
                  checkData();
                },),
                RaisedButton(child: Text('start'),onPressed: ()async{
                  isoltex = await createIsolate();
                },),
                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');
            }),
          ),
        );
      }
    }
    

      

    isolates.dart

    import 'dbhelper.dart';
    import 'package:flutter_isolate/flutter_isolate.dart';
    import 'dart:async';
    import 'dart:isolate';
    
    Future<FlutterIsolate> createIsolate() async {
    
      ReceivePort receivePort = ReceivePort();
    
      FlutterIsolate isolate = await FlutterIsolate.spawn(
        isolateEntry,
        receivePort.sendPort,
      );
      receivePort.listen((value){
        print('from spawrn side: $value');
      });
      return isolate;
    }
    
    
    isolateEntry(SendPort sendPort)async{
    
      DB db = DB();
      int i=0;
      while (i<40){
        var d = await db.addData('type', {'name':'$i'});
        i++;
    
        sendPort.send('ok $d');
        await Future.delayed(Duration(seconds: 1));
      }
    }
    

      

    another  https://blog.csdn.net/PD_Wang/article/details/80165265

  • 相关阅读:
    BZOJ 3811: 玛里苟斯 线性基
    HDU 3949 XOR 线性基
    BZOJ 2115: [Wc2011] Xor 线性基 dfs
    BZOJ 3963 HDU3842 [WF2011]MachineWorks cdq分治 斜率优化 dp
    BZOJ 3262: 陌上花开 cdq分治 树状数组
    BZOJ 2653: middle 主席树 二分
    BZOJ 3524 [Poi2014]Couriers 主席树
    BZOJ 4826: [Hnoi2017]影魔 单调栈 主席树
    BZOJ 3956: Count 主席树 可持久化线段树 单调栈
    2018/4/9省选模拟赛 0分
  • 原文地址:https://www.cnblogs.com/pythonClub/p/10816443.html
Copyright © 2011-2022 走看看