How do I do the equivalent of setTimeout + clearTimeout in Dart?
You can use the Timer class import ‘dart:async’; var timer = Timer(Duration(seconds: 1), () => print(‘done’)); timer.cancel();
You can use the Timer class import ‘dart:async’; var timer = Timer(Duration(seconds: 1), () => print(‘done’)); timer.cancel();
If I understand correctly: when you catch an exception in dart, you can also catch the stack trace: try { // something } catch(e, stacktrace) { myCompleter.completeException(e, stacktrace); }
import ‘dart:async’ show Stream; import ‘package:async/async.dart’ show StreamGroup; main() async { var s1 = stream(10); var s2 = stream(20); var s3 = StreamGroup.merge([s1, s2]); await for(int val in s3) { print(val); } } Stream<int> stream(int min) async* { int i = min; while(i < min + 10) { yield i++; } } See also http://news.dartlang.org/2016/03/unboxing-packages-async-part-2.html … Read more
This should sort your problem out. Basically you always want your Widgets created in your build method hierarchy. import ‘dart:async’; import ‘package:flutter/material.dart’; void main() => runApp(new MaterialApp(home: new Scaffold(body: new MainWidget()))); class MainWidget extends StatefulWidget { @override State createState() => new MainWidgetState(); } class MainWidgetState extends State<MainWidget> { List<ItemData> _data = new List(); Timer timer; … Read more
You can use CancelableOperation or CancelableCompleter to cancel a future. See below the 2 versions: Solution 1: CancelableOperation (included in a test so you can try it yourself): cancel a future test(“CancelableOperation with future”, () async { var cancellableOperation = CancelableOperation.fromFuture( Future.value(‘future result’), onCancel: () => {debugPrint(‘onCancel’)}, ); // cancellableOperation.cancel(); // uncomment this to test … Read more