🥚 Classes

Code your first Dart class

Create a Class

Classes are a way to group data and behavior together, like a blueprint for an Object.

file_type_dartlang classes.dart
class Basic {
  int id;

  Basic(this.id);

  doStuff() {
    print('Hello my ID is $id');
  }
}

Create an Object

Use the class to instantiate an Object. The new keyword is optional.

file_type_dartlang classes.dart
Basic thing = new Basic(55);
thing.id;
thing.doStuff();

Static Methods

You can call static methods on the class itself without creating a new object.

file_type_dartlang main.dart
class Basic {

  static globalData = 'global';
  static helper() {
      print('helper');
  }
}

Basic.globalData;
Basic.helper();

Questions? Let's chat

Open Discord