What is the main difference between:
Future<void> function(){}
Future<Null> function(){}
void function() {}
funtion(){}
Sometimes I use void or future when calling the API but I don't really know what the main difference is and when is the right time to use it?
A Future<T> instance produces a value of type T . If a future doesn't produce a usable value, then the future's type is Future<void> . A future can be in one of two states: uncompleted or completed. When you call a function that returns a future, the function queues up work to be done and returns an uncompleted future.
Future<Void> is a future result of an execution that returns no value. That would be typically the result of invoking the run method of a Runnable .
A Future class permits you to run work asynchronously to let loose whatever other threads ought not to be obstructed. Like the UI thread. In this blog, we will Explore Futures In Flutter. We will see how to use the future in your flutter applications.
That is where FutureBuilder comes in. You can use it when you have a future, to display one thing while you are waiting for it (for example a progress indicator) and another thing when it's done (for example the result). This is how you use a FutureBuilder to display the result of your future once you have it.
Yes, the main difference is that you can't await an async function that returns void, but you can await one that returns Future<void>. void does not implicitly become Future<void>. So you should basically always use Future<void>.
The way this is handled in Flutter / Dart is by using a Future. A Future allows you to run work asynchronously to free up any other threads that should not be blocked. Like the UI thread. A future is defined exactly like a function in dart, but instead of void you use Future.
The way this is handled in Flutter / Dart is by using a Future. A Future allows you to run work asynchronously to free up any other threads that should not be blocked. Like the UI thread. A future is defined exactly like a function in dart, but instead of void you use Future. If you want to return a value from the Future then you pass it a type.
It all goes back to that mind-boggling bottom type. You might consider Future<Null> for futures that must be awaitable but that never complete, or that always complete with an error. This is directly comparable to why a function would return Null. The same logic applies to Stream<Null>: Use this for a Stream that never sends any events.
Future<void> function() {}
Defines an asynchronous function that ultimately returns nothing but can notify callers when it eventually completes. Also see: What's the difference between returning void vs returning Future?
Future<Null> function() {}
Defines an asynchronous function that ultimately returns null
when it eventually completes. Don't use this; this is an archaic form of Future<void>
. It predates Dart 2 and was necessary because void
was not yet a proper type, and there was no mechanism for indicating that a Future
should return nothing. Also see: Dart 2: Legacy of the void
void function() {}
Defines a function that returns nothing. If the function does asynchronous work, callers will not be able to directly tell when it is complete.
function() {}
Defines a function with an unspecified return type. The return type is implicitly dynamic
, which means that the function can return anything. Don't do this since it does not convey intent; readers will not be able to tell if the return type was omitted intentionally or accidentally. It also will trigger the always_declare_return_types
lint. If you actually want to return a dynamic
type, you should explicitly use dynamic function() {}
instead.
function() {}
returns a null
pointer value.
Use this archetype when your function not return anything and it's not marked as async
:function() { // <- not async
// ... some code
// ... don't use return
}
void function() {}
sintaxis. It's same at the function() {}
but if you try to assign a value after calling you will get an error in compile time:Error: This expression has type 'void' and can't be used.
Personally, i recommend this approach if you really don't have a return value and the function it's not async.
Note that you can use void
in normal and async functions.
Future<void> function() async {}
is for asynchronous function thats returns a Future<void>
object.I personally recommend the void function() async {}
only if you don't use await
ot then
on call your function, otherwise, use Future <void> function() async {}
.
An examples:
Future<void> myFunction() async {
await Future.delayed(Duration(seconds: 2));
print('Hello');
}
void main() async {
print(myFunction()); // This works and prints
// Instance of '_Future<void>'
// Hello
// Use this approach if you use this:
myFunction().then(() {
print('Then myFunction call ended');
})
// or this
await myFunction();
print('Then myFunction call ended');
}
void myFunction() async {
await Future.delayed(Duration(seconds: 2));
print('Hello');
}
void main() async {
print(myFunction()); // This not works
// The compile fails and show
// Error: This expression has type 'void' and can't be used.
// In this case you only can call myFunction like this
myFunction();
// This doesn't works (Compilation Error)
myFunction().then(() {
print('Then myFunction call ended');
});
// This doesn't works (Compilation Error)
await myFunction();
print('Then myFunction call ended');
}
Future<Null> function() async {}
returns a Future<null>
object. Use it whenever you return null
. It's not recommended to use it because class Null
not extends from Object
and anything you return marks an error (except statements thats returns a null or explicit null value):Future<Null> myFunction() async {
await Future.delayed(Duration(seconds: 2));
print('Hello');
// Ok
return (() => null)();
// Ok
return null;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With