2

Can someone tell me why I am getting a 'null' output at the end of Result-1?

#When I return just 'Exercise1() from the main, the null do not appear. #But I am confused and want to know what is causing this null in the result.

main.dart

import 'exercises/exercise1.dart';

void main() {
  // return Exercise1();
  print(Exercise1());
}

exercise1.dart

import 'dart:io';

Exercise1() {
  stdout.write("What is your name? \n");
  String? name = stdin.readLineSync();

  stdout.write("Hi $name, What is your age? \n");
  int? age = int.parse(stdin.readLineSync().toString());

  int? yearsLeftFor100 = 100 - age;

  print("Hello $name, your age is $age");

  print("Hi $name, you will attain 100 years in another $yearsLeftFor100 years.");
}

Result-1: Run with print(Exercise1()) statement;

$ dart run main.dart
What is your name? 
John Doe
Hi John Doe, What is your age? 
31
Hello John Doe, your age is 31
Hi John Doe, you will attain 100 years in another 69 years.
null

Result-2: Run with return Exercise1() statement;

$ dart run main.dart
What is your name? 
John Doe
Hi John Doe, What is your age? 
31
Hello John Doe, your age is 31
Hi John Doe, you will attain 100 years in another 69 years.

2 Answers 2

2

Since you did not declare the return type of Exercise1(), it's return type is dynamic.

Since you do not have a return statement in your method, the return type is void. Meaning it does not return anything.

Therefore, when you are printing out the return value of it's invocation, you are seeing null.

4
  • So where I am wrong? Mar 27, 2022 at 7:54
  • You can just call your method Exercise1(), since from it's invocation, you are printing statements. If you want to return a value from the method, you need to change its signature (I.E. return a string). Mar 27, 2022 at 8:01
  • Got it. Which approach is better / correct ? Sorry, I am new to programming. Mar 27, 2022 at 8:09
  • Really depends on what you want to do with the value. If you need to do something with the value following the invocation, then you will need to return it. If not, you can keep your method void. If my answer helped you, please mark it as such. Mar 27, 2022 at 8:10
2

This is because Exercise1() is not "returning" anything. In other words, it's returning null value. If you want it to be "not null", you will have to return something in that function. Here's a fix. Lemme know if it works-

String Exercise1() {
  stdout.write("What is your name? \n");
  String? name = stdin.readLineSync();

  stdout.write("Hi $name, What is your age? \n");
  int? age = int.parse(stdin.readLineSync().toString());

  int? yearsLeftFor100 = 100 - age;

  return("Hello $name, your age is $age \n Hi $name, you will attain 100 years in another $yearsLeftFor100 years.");

}
1
  • It worked. Yay! Mar 27, 2022 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.