Flutter

What is constructor in flutter and how is used step by step guide

In Flutter (and Dart, the language behind Flutter), a constructor is a special method used to create and initialize an object when a class is instantiated. It allows you to pass values to the class when creating an instance.

What is a Constructor?

A constructor is a function with the same name as the class, used to set up an object.

Basic Syntax:

class ClassName {
  // Constructor
  ClassName() {
    // initialization code
  }
}

Types of Constructors in Dart

  1. Default Constructor
  2. Parameterized Constructor
  3. Named Constructor
  4. Factory Constructor

Step-by-Step Guide: Using a Constructor in Flutter

Step 1: Create a Dart class

Let’s say you’re building a User class.

class User {
  String name;
  int age;

  // Constructor
  User(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

this.name = name; means the class property name gets the value passed as name in the constructor.


Step 2: Create an Object Using the Constructor

You can now use the constructor to create an instance of the class:

void main() {
  User user1 = User("Alice", 25);
  print(user1.name); // Output: Alice
  print(user1.age);  // Output: 25
}

Alternative: Shorthand Constructor (Cleaner Syntax)

Dart allows a shorter way to write the constructor:

class User {
  String name;
  int age;

  User(this.name, this.age);
}

This does the same as the longer version above but is more concise.


Example in a Flutter Widget

Let’s see how constructors are used in Flutter widgets.

Step 1: Create a StatelessWidget with a Constructor

import 'package:flutter/material.dart';

class GreetingCard extends StatelessWidget {
  final String message;

  // Constructor
  GreetingCard({required this.message});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text(
        message,
        style: TextStyle(fontSize: 24),
      ),
    );
  }
}

Step 2: Use It in Your main.dart

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      body: GreetingCard(message: "Hello from Flutter!"),
    ),
  ));
}

Types of Constructors Explained

1. Default Constructor

class MyClass {
  MyClass() {
    print("Default constructor called");
  }
}

2. Parameterized Constructor

class MyClass {
  int x;
  MyClass(this.x);
}

3. Named Constructor

class MyClass {
  int x;
  MyClass.named(this.x);  // Named constructor
}

4. Factory Constructor (used for returning cached instances, etc.)

class Logger {
  static final Logger _instance = Logger._internal();

  factory Logger() {
    return _instance;
  }

  Logger._internal(); // private named constructor
}

Summary

TypeDescriptionSyntax
DefaultNo argumentsClassName()
ParameterizedTakes argumentsClassName(this.param)
NamedCustom named constructorsClassName.namedConstructor()
FactoryUsed when returning an existing objectfactory ClassName()

2 thoughts on “What is constructor in flutter and how is used step by step guide

Leave a Reply

Your email address will not be published. Required fields are marked *