From b504fd428c7277f3a53fd391604e25a099d7017f Mon Sep 17 00:00:00 2001 From: Bob Nystrom Date: Fri, 5 Aug 2016 15:18:07 -0700 Subject: [PATCH] Fix covariant overrides in SynchronousFuture. (#5262) * Fix covariant overrides in SynchronousFuture. There were two things going on here. In timeout(), the callback's return type was needlessly tightened to only allow callbacks that return futures. This makes SynchronousFuture not substitutable with Future, whose timeout() allows callbacks that return immediate values. Since SynchronousFuture.timeout() never calls the callback anyway, I just loosened it to match Future.timeout(). SynchronousFuture.whenComplete() is just wrong. The type error, again, is that the callback's return type is too tight. Future.whenComplete() allows synchronous callbacks. But the actual implementation is wrong as well. whenComplete() should return a future that completes to the *original value*, not whatever the callback returns. So I just fixed the method to work correctly, including handling callbacks with synchronous results. * "(error, stackTrace)" -> "(e, stack)". --- .../lib/src/foundation/synchronous_future.dart | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/foundation/synchronous_future.dart b/packages/flutter/lib/src/foundation/synchronous_future.dart index e5c0abb5f7..a635e2b763 100644 --- a/packages/flutter/lib/src/foundation/synchronous_future.dart +++ b/packages/flutter/lib/src/foundation/synchronous_future.dart @@ -42,8 +42,17 @@ class SynchronousFuture implements Future { } @override - Future timeout(Duration timeLimit, { Future onTimeout() }) => new Completer().future; + Future timeout(Duration timeLimit, { dynamic onTimeout() }) => new Completer().future; @override - Future whenComplete(Future action()) => action(); + Future whenComplete(dynamic action()) { + try { + dynamic result = action(); + if (result is Future) + return result.then((_) => _value); + return this; + } catch (e, stack) { + return new Future.error(e, stack); + } + } } \ No newline at end of file