What is class in Flutter(Dart) with example step by step

In Flutter, a class is a fundamental concept in Dart (the programming language used by Flutter) and is used to define the structure and behavior of objects. A class serves as a blueprint for creating objects (instances) and can contain fields (variables) and methods (functions) that define the properties and behaviors of those objects.

Understanding Classes in Flutter

  1. Definition of a Class:
  • A class encapsulates data for the object and methods to manipulate that data.
  • It can be thought of as a template or blueprint for creating objects.
  1. Components of a Class:
  • Fields: Variables that hold data for the class.
  • Methods: Functions that define the behavior of the class.
  • Constructors: Special methods used for initializing objects of the class.

Example: Creating and Using a Class in Flutter

Let’s go through a step-by-step example of defining and using a class in a Flutter application.

Step 1: Define a Class

Create a new file person.dart to define a class called Person.

// person.dart

class Person {
  // Fields
  String name;
  int age;

  // Constructor
  Person(this.name, this.age);

  // Method
  void introduce() {
    print('Hello, my name is $name and I am $age years old.');
  }
}

In this example:

  • Fields: name and age store the data for the Person object.
  • Constructor: Person(this.name, this.age) initializes the fields with values provided when a Person object is created.
  • Method: introduce() prints a message including the name and age of the Person.

Step 2: Use the Class in a Flutter Application

Modify the main.dart file to use the Person class and display information in a Flutter app.

// main.dart
import 'package:flutter/material.dart';
import 'person.dart';  // Import the file containing the Person class

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // Create an instance of Person
    Person person = Person('Alice', 30);

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Class Example'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'Person Info:',
                style: TextStyle(fontSize: 24),
              ),
              SizedBox(height: 20),
              Text(
                'Name: ${person.name}',
                style: TextStyle(fontSize: 20),
              ),
              Text(
                'Age: ${person.age}',
                style: TextStyle(fontSize: 20),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

In this example:

  • Importing Class: import 'person.dart'; imports the Person class.
  • Creating an Instance: Person person = Person('Alice', 30); creates an instance of Person.
  • Using the Instance: The name and age fields of the person object are displayed in the Flutter app’s UI.

Step-by-Step Summary

  1. Define the Class:
  • Create a file (e.g., person.dart).
  • Define the class with fields, a constructor, and methods.
  1. Use the Class:
  • Import the class into your main file (e.g., main.dart).
  • Create instances of the class.
  • Use the class’s fields and methods as needed in your Flutter widgets.

Additional Concepts

  • Inheritance: A class can inherit properties and methods from another class. This is useful for creating more specialized versions of a class.
  • Mixins: Allow you to add functionality to a class from multiple sources.
  • Abstract Classes: Define methods without implementations that must be overridden by subclasses.

Classes are a powerful way to structure your code in Flutter, helping you manage complex data and behaviors in a modular and reusable manner.

Related Articles:

Leave a Comment

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

Scroll to Top