Cambo Freelance
ទំព័រដើមសេវាកម្មអត្ថបទការបង្រៀនក្រុមវគ្គបណ្តុះបណ្តាលទំនាក់ទំនង
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

ផ្ញើសាររបស់អ្នក

ត្រៀមចាប់ផ្តើមគម្រោងរបស់អ្នក? ទំនាក់ទំនងក្រុមរបស់យើង។

ទំនាក់ទំនង

សេវាកម្ម

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

តំណភ្ជាប់មានប្រយោជន៍

  • ទំព័រដើម
  • សេវាកម្ម
  • ការសិក្សា
  • អំពីយើង
  • ទំនាក់ទំនង
  • តម្លៃ

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsDartStatic Members in Dart
🎯Dart Tutorials

97 lessons

Beginner(35)0Complete Dart Tutorial Series — Course Index1Introduction to Dart2Installing Dart SDK3Setting Up VS Code for Dart4DartPad Online Editor5Hello World in Dart6Dart Syntax7Comments in Dart8Variables in Dart9Data Types in Dart10Strings in Dart11Numbers in Dart12Booleans in Dart13Type Conversion in Dart14Constants in Dart: final and const15User Input in Dart16Arithmetic Operators in Dart17Assignment Operators in Dart18Comparison Operators in Dart19Logical Operators in Dart20Null-Aware Operators in Dart21If Statement in Dart22If Else in Dart23Else If in Dart25Switch Statement in Dart26Ternary Operator in Dart27For Loop in Dart28While Loop in Dart29Do While Loop in Dart30Break and Continue in Dart32Nested Loops in Dart33Functions in Dart35Optional Parameters in Dart37Arrow Functions in Dart38Recursive Functions in Dart
Intermediate(34)39Lists in Dart40Sets in Dart41Maps in Dart42Collection Operations in Dart
Advanced(28)73Futures in Dart74Async and Await in Dart75Streams in Dart76
🎯

Dart Tutorials

Lesson 54 of 97

All lessons
Beginner (35)0Complete Dart Tutorial Series — Course Index1Introduction to Dart
43
Spread Operator and Collection Literals in Dart
44Collection If in Dart
45Collection For in Dart
46Null Safety in Dart
47Nullable Variables in Dart
48Null Assertion Operator in Dart
49Late Keyword in Dart
50Required Keyword in Dart
51Classes and Objects in Dart
52Constructors in Dart
53Getters and Setters in Dart
54Static Members in Dart
55Interfaces in Dart
56Polymorphism in Dart
57Inheritance in Dart
58Method Overriding in Dart
59Abstract Classes in Dart
60Abstract Classes vs Interfaces in Dart
61Mixins in Dart
62Extension Methods in Dart
63Exception Handling in Dart
64Try-Catch in Dart
65Finally Block in Dart
66Custom Exceptions in Dart
67Generics in Dart
68Typedef in Dart
69Enums in Dart
70Records in Dart
71Pattern Matching in Dart
72Cascade Notation in Dart
Stream Controllers in Dart
77Isolates in Dart
78File Operations in Dart
80JSON Parsing in Dart
82HTTP Requests in Dart
83REST API Client in Dart
84Dart Packages and pub.dev
85Testing in Dart
86Dart CLI Applications
87Dart Code Style and Best Practices
88Server-side Dart with Shelf
89Introduction to Flutter
90Flutter Widgets and Layouts
91Flutter State Management
92Flutter Navigation and Routing
93Project: Calculator in Dart
94Project: Temperature Converter in Dart
95Project: Age Calculator in Dart
96Project: Todo CLI App in Dart
97Project: Student Management System in Dart
98Project: Expense Tracker in Dart
100Project: Banking System in Dart
101Project: Inventory Management System in Dart
102Project: Contact Manager in Dart
103Project: Mini REST API Backend in Dart
2
Installing Dart SDK
3Setting Up VS Code for Dart
4DartPad Online Editor
5Hello World in Dart
6Dart Syntax
7Comments in Dart
8Variables in Dart
9Data Types in Dart
10Strings in Dart
11Numbers in Dart
12Booleans in Dart
13Type Conversion in Dart
14Constants in Dart: final and const
15User Input in Dart
16Arithmetic Operators in Dart
17Assignment Operators in Dart
18Comparison Operators in Dart
19Logical Operators in Dart
20Null-Aware Operators in Dart
21If Statement in Dart
22If Else in Dart
23Else If in Dart
25Switch Statement in Dart
26Ternary Operator in Dart
27For Loop in Dart
28While Loop in Dart
29Do While Loop in Dart
30Break and Continue in Dart
32Nested Loops in Dart
33Functions in Dart
35Optional Parameters in Dart
37Arrow Functions in Dart
38Recursive Functions in Dart
Intermediate (34)39Lists in Dart40Sets in Dart41Maps in Dart42Collection Operations in Dart43Spread Operator and Collection Literals in Dart44Collection If in Dart45Collection For in Dart46Null Safety in Dart47Nullable Variables in Dart48Null Assertion Operator in Dart49Late Keyword in Dart50Required Keyword in Dart51Classes and Objects in Dart52Constructors in Dart53Getters and Setters in Dart54Static Members in Dart55Interfaces in Dart56Polymorphism in Dart57Inheritance in Dart58Method Overriding in Dart59Abstract Classes in Dart60Abstract Classes vs Interfaces in Dart61Mixins in Dart62Extension Methods in Dart63Exception Handling in Dart64Try-Catch in Dart65Finally Block in Dart66Custom Exceptions in Dart67Generics in Dart68Typedef in Dart69Enums in Dart70Records in Dart71Pattern Matching in Dart72Cascade Notation in Dart
Advanced (28)73Futures in Dart74Async and Await in Dart75Streams in Dart76Stream Controllers in Dart77Isolates in Dart78File Operations in Dart80JSON Parsing in Dart82HTTP Requests in Dart83REST API Client in Dart84Dart Packages and pub.dev85Testing in Dart86Dart CLI Applications87Dart Code Style and Best Practices88Server-side Dart with Shelf89Introduction to Flutter90Flutter Widgets and Layouts91Flutter State Management92Flutter Navigation and Routing93Project: Calculator in Dart94Project: Temperature Converter in Dart95Project: Age Calculator in Dart96Project: Todo CLI App in Dart97Project: Student Management System in Dart98Project: Expense Tracker in Dart100Project: Banking System in Dart101Project: Inventory Management System in Dart102Project: Contact Manager in Dart103Project: Mini REST API Backend in Dart
🎯DartIntermediateLesson 54 of 97Dart TeamJune 19, 2026

Static Members in Dart

Master Dart static members — static fields shared across instances, static utility methods, static constants, factory methods, and the singleton pattern.


Table of Contents

  1. What are Static Members?
  2. Static Fields
  3. Static Methods
  4. Static Constants
  5. Static Factory Methods
  6. Full Example
  7. Exercise
  8. Quiz
  9. Summary

What are Static Members?

Static members belong to the class itself rather than to any specific instance (object). They are shared across all instances of the class. Declare them with the static keyword. Access them using the class name, not an object reference.

Static Fields

class Counter {
  // Static field — shared across ALL Counter objects
  static int _count = 0;

  final int id;

  Counter() : id = ++_count;

  static int get count => _count;
  static void reset() => _count = 0;
}

void main() {
  print(Counter.count);   // 0
  var a = Counter();
  var b = Counter();
  var c = Counter();
  print(Counter.count);   // 3
  print(a.id);            // 1
  print(b.id);            // 2
  Counter.reset();
  print(Counter.count);   // 0
}

Static Methods

Static methods have no access to this — they cannot reference instance fields or methods. They are utility functions tied logically to the class:

class MathUtils {
  // Cannot be instantiated — all static
  MathUtils._();  // private constructor

  static double square(double x) => x * x;
  static double cube(double x) => x * x * x;
  static double clamp(double value, double min, double max) =>
      value < min ? min : (value > max ? max : value);
  static bool isPrime(int n) {
    if (n < 2) return false;
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0) return false;
    }
    return true;
  }
}

void main() {
  print(MathUtils.square(5));        // 25.0
  print(MathUtils.cube(3));          // 27.0
  print(MathUtils.clamp(150, 0, 100));  // 100.0
  print(MathUtils.isPrime(17));      // true
  print(MathUtils.isPrime(15));      // false
}

Static Constants

class Config {
  static const String appName = ''CamboFreelance'';
  static const String version = ''2.0.0'';
  static const int maxRetries = 3;
  static const Duration timeout = Duration(seconds: 30);
  static const Map<String, String> headers = {
    ''Content-Type'': ''application/json'',
    ''Accept'': ''application/json'',
  };
}

void main() {
  print(Config.appName);          // CamboFreelance
  print(Config.version);          // 2.0.0
  print(Config.timeout.inSeconds); // 30
}

Static Factory Methods

A common pattern is static factory methods — named constructors that return instances with validation or special initialization:

class Color {
  final int r, g, b;

  const Color(this.r, this.g, this.b);

  // Static factory methods for named colors
  static const Color red   = Color(255, 0, 0);
  static const Color green = Color(0, 255, 0);
  static const Color blue  = Color(0, 0, 255);
  static const Color white = Color(255, 255, 255);
  static const Color black = Color(0, 0, 0);

  static Color fromHex(String hex) {
    hex = hex.replaceFirst(''#'', '''');
    return Color(
      int.parse(hex.substring(0, 2), radix: 16),
      int.parse(hex.substring(2, 4), radix: 16),
      int.parse(hex.substring(4, 6), radix: 16),
    );
  }

  @override
  String toString() => ''Color(r=$r, g=$g, b=$b)'';
}

void main() {
  print(Color.red);              // Color(r=255, g=0, b=0)
  print(Color.fromHex(''#1A2B3C''));  // Color(r=26, g=43, b=60)
}

Full Example: Singleton Pattern

class AppLogger {
  // Static field holds the single instance
  static AppLogger? _instance;
  static int _logCount = 0;

  // Private constructor
  AppLogger._();

  // Static getter returns the single instance
  static AppLogger get instance {
    _instance ??= AppLogger._();
    return _instance!;
  }

  void log(String level, String message) {
    _logCount++;
    final timestamp = DateTime.now().toIso8601String();
    print(''[$timestamp][$level] $message'');
  }

  void info(String msg) => log(''INFO'', msg);
  void error(String msg) => log(''ERROR'', msg);

  static int get logCount => _logCount;
}

void main() {
  var logger = AppLogger.instance;
  logger.info(''App started'');
  logger.error(''Something went wrong'');
  AppLogger.instance.info(''Same instance'');
  print(''Total logs: \$${AppLogger.logCount}'');
}
Output:
[timestamp][INFO] App started
[timestamp][ERROR] Something went wrong
[timestamp][INFO] Same instance
Total logs: 3

Exercise

  1. Create a StringUtils class with only static methods: capitalize(), isPalindrome(), wordCount(), and reverse().
  2. Implement a UserIdGenerator class with a static counter that auto-increments each time a new user ID is requested via a static method nextId().

Quiz

1. How do you access a static method?

  • A) Through an instance: obj.method()
  • B) Through the class name: ClassName.method() ✅
  • C) Via the super keyword
  • D) Using the static keyword at the call site

2. Can a static method access instance fields?

  • A) Yes, directly
  • B) Yes, via this
  • C) No — static methods have no this reference ✅
  • D) Only if they are public

3. What keyword makes a class-level constant in Dart?

  • A) final
  • B) const (but not static)
  • C) static const ✅
  • D) readonly

Summary

Static members belong to the class, not to instances. Static fields are shared across all objects of the class. Static methods cannot use this and are accessed via the class name. Use static const for compile-time constants. Common patterns include utility classes (all static methods), singleton instances (one shared object), and static factory methods for named constructors.

dartprogrammingintermediateoop
PreviousLesson 53: Getters and Setters in DartNextLesson 55: Interfaces in Dart
Back to All Tutorials