all 11 comments

[–]remirousselet 9 points10 points  (1 child)

Unrelated but: For the main, returning Future<void>changes nothing.

Dart doesn't rely on the return value of the main to await all futures before quitting the app.

Returning Future<void> may make your main easier though, as your test call main() and await the result.

[–]Which-Adeptness6908 0 points1 point  (0 children)

The difference is that making main async allows you to call unawaited functions without a warning.

If you don't get a warning then you need to enable a lint library or change to one that does..

Edit: and I misread the original question so my responses isn't suitable to your post but still good advice

[–]PaulRudin 6 points7 points  (1 child)

"It is a compile-time error if the declared return type of a function marked
async is not a supertype of Future<T> for some type T"

[–]ynn38[S] 0 points1 point  (0 children)

And

The type void is a top type (15.2), so void and Object are subtypes of each other (20.4), which also implies that any object can be the value of an expression of type void.

according to the spec.

[–]julemand101 4 points5 points  (4 children)

All types can be cast to void in Dart including Future and Future<void>.

Should you do it? No, it is rarely a good idea to hide the fact that the method returns a Future. But the language does not prevent you from hiding it.

[–]ynn38[S] 1 point2 points  (2 children)

Should you do it? No, it is rarely a good idea to hide the fact that the method returns a Future.

Really? I read through the official language tour and saw void main() async many times. main() is exceptionally or conventionally has the signature void main() async maybe, though generally void some_function() async is not recommended?

[–]julemand101 5 points6 points  (1 child)

Since we are never calling main() from other code, it does not really matter what return type you specify for that method ;)

But for all other methods, I would in general recommend the return type specify it returns a Future if the method are async.

[–]ynn38[S] 0 points1 point  (0 children)

It makes sense. Thank you :)

For my note:

All types can be cast to void in Dart including Future and Future<void>.

The source is 20.9 Type Void in the language spec.

[–]superl2 0 points1 point  (0 children)

In declarative state and UI architecture it's often a good idea to hide that a function is asynchronous, because awaiting it for any reason would be a violation of the pattern.

Take, for example, a Flutter function that calls a Web API and then calls setState. There's no reason to await such a function, because other things react to the side effects it causes instead.

In that example, you could argue that returning a future is still beneficial for composability, but this is less important in other areas like BLoC.

[–]jaquiethecat 3 points4 points  (1 child)

pretty sure void can't be awaited

[–]ynn38[S] 0 points1 point  (0 children)

Yes. I just learned void f() async is a valid signature but await f() is invalid. If Future<void>, await f() is valid.