Home > database >  void function not printing because cant return value but when i make string or int function it print
void function not printing because cant return value but when i make string or int function it print

Time:10-22

New to dart, take following code as example, //void function doesnt work and int/string function either returns null that is printed or value return which is automatically printed as can be seen in the output.

class Microphones{
  String? name;
  String? color;
  int? model;
  String printEverything(){
    print(name);
    return " "; 
  }
  int? printint(){ 
    print(model); 
}
}

void main() {
  var mic1=Microphones();
  print(mic1.name);
  mic1.name="Yeti";
  mic1.color="black";
  mic1.model=26;

  print(mic1.name);
  print(mic1.color);
  print(mic1.model);
  print(mic1.printEverything());
  print(mic1.printint());
}

output:
null
Yeti
black
26
Yeti
 
26
null

i highly appreciate your replies/help in this regard.

CodePudding user response:

All methods in Dart retuns null by default if you are not returning anything (either by having an empty return in methods specified to return void or not having any return at all in methods returning void or a nullable type like int?).

But return-type in the method signature specifies how the caller of the method should see the value. And here we have void which is described in the Dart language tour as:

A special type that indicates a value that’s never used.

https://dart.dev/guides/language/language-tour#a-basic-dart-program

So we specify void to signal to the user of our method, that the returned value should never be used. And the Dart analyzer and compiler will then try very hard to make sure you are not ending up using the returned value typed void.

Advanced section for the curious

But that does not mean it is impossible to use a returned value specified to be void by the method signature:

void someMethod() {
  void someVoidVariable = 'Hey, I am a String!';
  return someVoidVariable;
}

void someOtherMethod() => 'Hey, I am also a String!';

void main() {
  print(someMethod() as String); // Hey, I am a String!
  print(someOtherMethod() as String); // Hey, I am also a String!
}

In the first example, we force Dart to see our String as void. That is completely fine since all types can be seen as void. We are then allowed to return this void value since someMethod() is suppose to return void.

The reason for this forcing is that Dart would complain about (which makes sense because what we are doing is very much stupid and should never be in a real program):

void someMethod() {
  return 'Hey, I am a String!';
  // ERROR: A value of type 'String' can't be returned from the function 
  // 'someMethod' because it has a return type of 'void'.
}

Or this since we can't cast to void using as:

void someMethod() {
  return 'Hey, I am a String!' as void; // ERROR: Expected a type name.
}

We are then casting that void back to a String to let Dart allow us to use the returned value in print() (since Dart otherwise would prevent us from trying to use the void typed value).

In the second example, I show the one of the reasons for why things are working like they does. In Dart, we can write these two method which are going to do the exact same (note I changed the return type to String for this example):

String someOtherMethod() {
  return 'Hey, I am also a String!';
}

String someOtherMethod() => 'Hey, I am also a String!';

So the second way is a shorthand for if we just want to execute a single statement and return its value.

But what about:

void someOtherMethod() => 'Hey, I am also a String!';

If we followed the traditional rules, this would not be allowed since we cannot return a String since we have typed void. But sometimes, we also want to use this simple syntax to just execute a method and don't want to care about what that method would end up returning.

E.g. this:

final someList = <String>[];
void removeStringFromList(String string) => someList.remove(string);

Where the remove method on List is actually returning a bool which indicate if the element was inside the list.

So Dart ignores this problem when we use the => by just casting the result to void in this case. So in our last example, the returned bool is actually returned to the caller of removeStringFromList but because its type has been cast to void, the caller of removeStringFromList is prevented (in a lot of ways) from using the value.

  • Related