Blogs By Shankar
Dart
Dart
  • Dart Tutorial by Dès Vu Technologies
  • hello
  • Dart Collection
    • collectionInDart
  • File Handeling
    • fileHandelingInDart
  • Functions
    • functionParameters
    • Types of Functions in Dart
    • Functions in Dart
    • Annonymous Function
      • AnnonymousFunction
    • Arrow Function
      • arrowFunctions
    • BuiltinFunctions
      • importantBuiltinFunctions
      • mathFunction
  • OOP With Dart
    • encapsulation
    • oopWithDart
    • Generic In Dart
      • generic
    • constructor
      • constructor
      • factory constructor
        • factoryConstructor
      • initializer list constructor
        • Initializer List Constructor:
    • async dart
      • asyncAndAwait
        • asyncAndAwait
      • future
        • future
      • streams
        • streams
  • Sync and Async dart
    • syncAndAsyncDart
  • controlFlow
    • controlFlow
  • dataTypes
    • Dart Built-In Data Types
    • TypeConversion
    • String
      • stringOperations
      • string_jnterpolation
    • operators
      • Operators In Dart
  • operators
    • operators
  • user_input
    • userInput
  • variablesAndConstants
    • scopeInDart
    • variableTypesInDart
    • variables
Powered by GitBook
On this page
  1. OOP With Dart
  2. constructor

constructor

Constructors in Dart are special methods used for creating and initializing objects of a class. They are crucial for setting up the initial state of objects. Dart provides several types of constructors, each serving a specific purpose:

  1. Default Constructor:

    • If you don't explicitly define any constructors in your class, Dart provides a default constructor with no arguments.

    • The default constructor initializes fields with their default values (e.g., null for objects, 0 for integers, false for booleans).

    class Person {
      String name;
      int age;
    }
    
    void main() {
      var person = Person();
      print('${person.name} ${person.age}'); // Output: null 0
    }
  2. Parameterized Constructor:

    • A parameterized constructor allows you to initialize class fields by providing parameters when creating an object.

    class Person {
      String name;
      int age;
    
      // Parameterized constructor
      Person(this.name, this.age);
    }
    
    void main() {
      var person = Person('Alice', 30);
      print('${person.name} ${person.age}'); // Output: Alice 30
    }
  3. Named Constructor:

    • Named constructors are used to define additional named constructors in a class.

    • They provide alternative ways to create objects with specific configurations.

    class Person {
      String name;
      int age;
    
      // Named constructor
      Person.guest() {
        name = 'Guest';
        age = 18;
      }
    }
    
    void main() {
      var person = Person.guest();
      print('${person.name} ${person.age}'); // Output: Guest 18
    }
  4. Factory Constructor:

    • Factory constructors are used when you want to control the object creation process.

    • They may return an instance of the class, potentially from a cache or by reusing an existing object.

    class Singleton {
      static Singleton _instance;
    
      // Private constructor
      Singleton._();
    
      factory Singleton.getInstance() {
        if (_instance == null) {
          _instance = Singleton._();
        }
        return _instance;
      }
    }
    
    void main() {
      var instance1 = Singleton.getInstance();
      var instance2 = Singleton.getInstance();
    
      print(identical(instance1, instance2)); // Output: true (Both references point to the same object)
    }
  5. Initializer List Constructor:

    • An initializer list constructor allows you to perform additional initialization of fields before the constructor's body runs.

    class Point {
      final int x;
      final int y;
    
      // Initializer list constructor
      Point(this.x, this.y) : assert(x >= 0), assert(y >= 0);
    }
    
    void main() {
      var point = Point(2, 3);
      print('Point: ${point.x}, ${point.y}'); // Output: Point: 2, 3
    }
  6. Constant Constructor:

    • Constant constructors are used to create constant objects. The const keyword is used to invoke them.

    • Objects created with a constant constructor are guaranteed to be identical and immutable at compile-time.

    class Circle {
      final double radius;
    
      // Constant constructor
      const Circle(this.radius);
    
      double get area => 3.14159265359 * radius * radius;
    }
    
    void main() {
      const Circle circle1 = Circle(5.0);
      const Circle circle2 = Circle(5.0);
    
      print(circle1.area); // Output: 78.539816339745
      print(identical(circle1, circle2)); // Output: true (Both objects are the same)
    }

These are the primary types of constructors in Dart, each serving different purposes and allowing you to create and initialize objects in various ways to suit your application's needs.

PreviousconstructorNextfactory constructor

Last updated 1 year ago