Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Future<void> vs Future<Null> vs void

Tags:

flutter

dart

What is the main difference between:

  1. Future<void> function(){}
  2. Future<Null> function(){}
  3. void function() {}
  4. 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?

like image 851
Nazarudin Avatar asked Apr 16 '21 03:04

Nazarudin


People also ask

What is Future void in flutter?

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.

What is Future void?

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 .

What does Future mean in flutter?

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.

How do you deal with Future in flutter?

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.

What is the difference between future<void> and future< void>?

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>.

What is the use of future in flutter?

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.

How to return a value from a future in flutter/Dart?

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.

Why does future<null> return null?

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.


2 Answers

  1. 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?

  2. 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

  3. 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.

  4. 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.

like image 123
jamesdlin Avatar answered Nov 10 '22 16:11

jamesdlin


  1. By default, a function likes 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
}
  1. Optionally, you can specify not return values using 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.

  1. A function likes 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');
}
  1. A function likes 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;
}
like image 33
Ignacior Avatar answered Nov 10 '22 16:11

Ignacior