0
votes

what is the difference between void example(); and void example; in abstract class in Dart Language?

abstract class Test
{
void example1();
void example2;
}
1

1 Answers

0
votes

void means that the value is not to be used and should therefore not be used. In most cases, we mark a method to return void when we want to indicate, that our method does not return anything.

So example1() is a method which signature indicates that the returned value is not to be used.

example2 is a variable of the type void. This is really not that useful since we are to use the value of this variable since the type is void which, as previous described, indicates the value should not be used.

But because void is used as a type in Dart, you are allowed to have a variable of the type void even if it does not really make much sense.

If you really want to do something with it, you can do something strange as:

abstract class Test
{
  void example1();
  void example2;
}

class A extends Test {
  @override
  void example1() => throw UnimplementedError();

  @override
  void get example2 => print('Hello World');
}

void main() {
  A().example2;
}

Here we cannot use the value from example2 for anything but we can still call some code when trying to access the property.

There are lot more details about the void type in the Dart Language Specification in the section "Type Void": https://dart.dev/guides/language/specifications/DartLangSpec-v2.10.pdf