10

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?

Nazarudin
  • 917
  • 8
  • 26

2 Answers2

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

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
3
  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;
}
Ignacior
  • 897
  • 2
  • 15