Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Static Members


Share

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


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.