Classes Code your first Dart class

This lesson is available for PRO members or as a single course purchase. Sign-in and choose a plan below.
Get Unlimited PRO Access

OR


*Enrollment provides full access to this course (and updates) for life.

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();

Q&A Chat