Reputation: 1248
For some reason the compiler is complaining about this:
SynchronousFuture<void> setNewRoutePath(AppLink newLink) {
_currentLink = newLink;
// return null; // compiler really wants to see this null return...why?
}
But is happy with this:
Future<void> setNewRoutePath(AppLink newLink) async {
_currentLink = newLink;
}
Seems like async
keyword is handling the implicit return here. Is there some equivalent for SynchronousFuture
?
Upvotes: 1
Views: 954
Reputation: 89936
SynchronousFuture<void> setNewRoutePath(AppLink newLink) { _currentLink = newLink; // return null; // compiler really wants to see this null return...why? }
You need a return value because your function is declared to return a non-void
value, so it must return something. You could argue that all functions could implicitly return null
if there's no explicit return
statement, but that would be error-prone:
int f(String someValue) {
if (someCondition) {
return 42;
}
} // f didn't return anything if someCondition is false. Accidental or intentional?
Future
s themselves aren't really special. If you have a non-async
function that returns a Future
, you must still have an explicit return
statement:
Future<void> f() {
print('Hello world!');
} // Error: f doesn't end with a return statement.
However, the async
keyword does a few things:
await
keyword.Future
. This includes implicit return values from void
functions.SynchronousFuture
is just an ordinary class provided by Flutter that implements the Future
interface. It is not special. It's not part of the Dart language, so there is not going to be any keyword that does automatic return value wrapping like what the async
keyword does.
Also note that in your function that returns a SynchronousFuture
, you should not just sprinkle return null
statements. With an async
function, a return;
statement (or exiting the function without an explicit return
statement) ultimately returns a Future<void>()
to the caller. Callers expect to be able to call methods (e.g. .then()
) on the returned Future
to add completion callbacks. If you return null
for a SynchronousFuture
, calling .then()
on it will result in a null pointer exception at runtime. You instead would need to use return SynchronousFuture<void>(null);
.
I also should point out that the SynchronousFuture
documentation states:
In general use of this class should be avoided as it is very difficult to debug such bimodal behavior.
so unless you have some clear need to use a SynchronousFuture
, you're better off avoiding it.
Upvotes: 4
Reputation: 6337
Unless you have created a class called SynchronousFuture
the compiler will complain as there is no class called SynchronousFuture
in the dart standard library, so either you or a library you import must define this class.
In dart
all async
functions must have a return value of type Future<T>
.
// return null; // compiler really wants to see this null return...why?
Because Future
is not the same as void
, when you return a value in an async function, that value is subsequently used as value for the future, as future is a generic class.
Upvotes: 4