metadeck
10/5/2018 - 9:51 AM

Beginners Dart - Classes

Beginners Dart - Classes

class TheClass { //Here I have created the class.

   //This function prints the word hello and if passed a string.
  void sayWord(String name) => print("Hello ${name}");

  //This is a basic function that returns the sum of 5 times five.
  int calculate() => 5 * 5; 

  //A quick thing to note is that a fat arrow in the code (=>) is a quick way of creating a line function, it is just the same as a regular function except uses an extra symbol and takes up less space.

}

void main() {

  //We now create the class for our use.
  TheClass myClass = new TheClass();

  //Here we pass a value to a funtion within the class and have it print the words "Hello Bob".
  myClass.sayWord("Bob");
  //Finally in the class it returns a value of 25 when this function is called, so we run the function and print the value returned.
  print(myClass.calculate());

}