Created
April 16, 2019 19:12
-
-
Save jebright/a7086adc305615aa3a655c6d8bd90264 to your computer and use it in GitHub Desktop.
Using an Isolate in Flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'dart:isolate'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'Flutter Isolate Demo', | |
theme: new ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: new MyHomePage(title: 'Flutter Isolates'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => new _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
Isolate _isolate; | |
bool _running = false; | |
static int _counter = 0; | |
String notification = ""; | |
ReceivePort _receivePort; | |
void _start() async { | |
_running = true; | |
_receivePort = ReceivePort(); | |
_isolate = await Isolate.spawn(_checkTimer, _receivePort.sendPort); | |
_receivePort.listen(_handleMessage, onDone:() { | |
print("done!"); | |
}); | |
} | |
static void _checkTimer(SendPort sendPort) async { | |
Timer.periodic(new Duration(seconds: 1), (Timer t) { | |
_counter++; | |
String msg = 'notification ' + _counter.toString(); | |
print('SEND: ' + msg); | |
sendPort.send(msg); | |
}); | |
} | |
void _handleMessage(dynamic data) { | |
print('RECEIVED: ' + data); | |
setState(() { | |
notification = data; | |
}); | |
} | |
void _stop() { | |
if (_isolate != null) { | |
setState(() { | |
_running = false; | |
notification = ''; | |
}); | |
_receivePort.close(); | |
_isolate.kill(priority: Isolate.immediate); | |
_isolate = null; | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text(widget.title), | |
), | |
body: new Center( | |
child: new Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
new Text( | |
notification, | |
), | |
], | |
), | |
), | |
floatingActionButton: new FloatingActionButton( | |
onPressed: _running ? _stop : _start, | |
tooltip: _running ? 'Timer stop' : 'Timer start', | |
child: _running ? new Icon(Icons.stop) : new Icon(Icons.play_arrow), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello! Thanks for sharing this! I was having difficulties to run a simple timer in background. Your sample code was useful! Based on it, I created this counter with the possibility to pass an initial time as parameter. I hope this can help other people too.