The code gives an output of null instead of desired output, How can I fix the problem?

Viewed 19
void main() {
  // Object? obj = "Pre Null Safe Dart";
  // showString(obj as String);
  print(Rectangle().calculateArea());
  print(Coffee().checkTemp());
}

class Rectangle {
  double? height;
  double? length;
  double? breadth;
  constructor() {
    length = 10.7;
    height = 6;
    breadth = 45;
  }

The values where set to null

  calculateArea() {
    if (length != null && height != null) {
      return length! * height!;
    }
  }

  calcualteVolume() {
    if (length != null && height != null && breadth != null) {
      return length! * height! * breadth!;
    }
  }
}

I think it because the variables in the class were set to null that is why I am getting the output.

class Coffee {
  String? temperature;

  void heat() {
    temperature = 'hot';
  }

  void chill() {
    temperature = 'cold';
  }

  checkTemp() {
    if (temperature != null) {
      print('Ready to surf' + temperature! + "!");
    }
  }

  String surf() => temperature! + "coffee";
}

Connecting to VM Service at http://127.0.0.1:58237/Gt-9FUCQakA=/ 2 null Exited

That is the output above
0 Answers
Related