Dart Static Members
Dart Static Members
Normal fields and methods belong to an object.
Each object has its own copy of the fields.
A static field or method belongs to the class itself.
There is only one copy, and it is shared by all objects of the class.
You create a static member with the static keyword.
You use it with the class name, not with an object.
Static Fields
A static field is a variable that belongs to the class.
In this example, the class counts how many cars have been created:
Example
class Car {
static int count = 0;
String brand;
Car(this.brand) {
count++;
}
}
void main() {
var car1 = Car('Volvo');
var car2 = Car('BMW');
var car3 = Car('Ford');
print(car1.brand);
print(Car.count);
}
Try it Yourself »
Result:
Volvo
3
Every car has its own brand, but all cars share the same count.
Inside the class, you can write just count, without the class name.
You cannot read a static field through an object:
Example
class Car {
static int count = 0;
}
void main() {
var car = Car();
print(car.count);
}
Try it Yourself »
Result:
main.dart:7:13: Error: The getter 'count' isn't defined for the type 'Car'.
- 'Car' is from 'main.dart'.
Try correcting the name to the name of an existing getter, or defining a getter or field named 'count'.
print(car.count);
^^^^^
Static Methods
A static method is a method that belongs to the class.
You can call it without creating an object first.
Static methods are useful for helper functions that are related to the class:
Example
class Calculator {
static int add(int a, int b) {
return a + b;
}
static int multiply(int a, int b) => a * b;
}
void main() {
print(Calculator.add(5, 3));
print(Calculator.multiply(5, 3));
}
Try it Yourself »
Result:
8
15
A static method has no object, so it cannot use this or the normal fields of the class.
It can only use static members and its own parameters:
Example
class Student {
String name = 'Liam';
static void showName() {
print(name);
}
}
void main() {
Student.showName();
}
Try it Yourself »
Result:
main.dart:5:11: Error: Undefined name 'name'.
print(name);
^^^^
Static Constants
A static const field is a constant that belongs to the class.
Its value is set when you write the code, and it can never change.
Use it for fixed values that are related to the class:
Example
class Game {
static const int maxPlayers = 4;
static const String title = 'Space Race';
}
void main() {
print(Game.title);
print('Max players: ${Game.maxPlayers}');
}
Try it Yourself »
Result:
Space Race
Max players: 4
Note: Inside a class, a constant must be static const.
A field that is only const, without static, is an error.
Static and Normal Members Together
A normal method can use static members.
This example uses a static constant and a static field together with normal fields:
Example
class Ticket {
static const double price = 12.5;
static int sold = 0;
String movie;
int seats;
Ticket(this.movie, this.seats) {
sold += seats;
}
double total() => seats * price;
}
void main() {
var t1 = Ticket('Frozen', 2);
var t2 = Ticket('Cars', 3);
print(t1.total());
print(t2.total());
print('Tickets sold: ${Ticket.sold}');
}
Try it Yourself »
Result:
25.0
37.5
Tickets sold: 5