Classes

Classes in Dart work a lot like classes in other object-oriented languages, but here’s most of what you need to know to get started.

Members: Properties and Methods

Properties are variables declared as members of a class:

 class​ User {
 int​ id;
 String​ username;
 String​ password;
 }

Methods are functions defined inside classes. They are defined just like top-level functions, but inside the body of a class definition:

 class​ User {
 int​ id;
 String​ username;
 String​ password;
 
 void​ setId(​int​ newId) {
  id = newId;
  }
 }

Accessing Members: Dot Notation

Members can be accessed using dot notation. If you wanted to get the id property of the user object and assign it to the a variable, you’d write:

 var​ a ...

Get Programming Flutter now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.