Classes help you organize your app by grouping related data and actions together. Objects are like real things made from these blueprints.
When you want to represent a real-world thing like a user or a product in your app.
When you need to keep data and the actions on that data together in one place.
When you want to create many similar items with different details easily.
When your app grows and you want to keep your code neat and easy to understand.
Syntax
Flutter
class ClassName {
// properties
Type propertyName;
// constructor
ClassName(this.propertyName);
// methodvoid methodName() {
// code
}
}
// Creating an object
var objectName = ClassName(value);
Use class keyword to define a class.
Constructor sets initial values when you create an object.
Examples
This defines a Person class with name and age. The sayHello method prints a greeting.
Flutter
class Person {
String name;
int age;
Person(this.name, this.age);
void sayHello() {
print('Hello, my name is $name.');
}
}
var p = Person('Anna', 25);
p.sayHello();
This Car class has a brand and a method to honk.
Flutter
class Car {
String brand;
Car(this.brand);
void honk() {
print('$brand says Beep!');
}
}
var myCar = Car('Toyota');
myCar.honk();
Sample App
This Flutter app shows how to create a Person object and display a greeting using its method.
Flutter
import'package:flutter/material.dart';
class Person {
String name;
int age;
Person(this.name, this.age);
String greet() {
return'Hi, I am $name and I am $age years old.';
}
}
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
var person = Person('Liam', 30);
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Classes and Objects')),
body: Center(
child: Text(person.greet(), style: const TextStyle(fontSize: 20)),
),
),
);
}
}
OutputSuccess
Important Notes
Objects are created from classes using constructors.
Methods inside classes define what actions the object can do.
Use clear names for classes and properties to keep code readable.
Summary
Classes are blueprints for creating objects.
Objects hold data and can perform actions defined by their class.
Using classes helps organize and reuse code in your app.
Practice
(1/5)
1. What is a class in Flutter programming?
easy
A. A type of widget
B. A blueprint to create objects
C. A function that runs the app
D. A variable that stores data
Solution
Step 1: Understand the role of a class
A class defines a template or blueprint for creating objects with properties and behaviors.
Step 2: Differentiate from other concepts
Widgets, functions, and variables serve different purposes; only a class is a blueprint for objects.
Final Answer:
A blueprint to create objects -> Option B
Quick Check:
Class = blueprint for objects [OK]
Hint: Classes define blueprints, not widgets or variables [OK]
Common Mistakes:
Confusing classes with widgets
Thinking classes are functions
Mixing classes with variables
2. Which of the following is the correct way to define a simple class named Car in Dart?
easy
A. function Car() { }
B. Car class { }
C. def Car() { }
D. class Car { }
Solution
Step 1: Recall Dart class syntax
In Dart, classes are defined using the keyword class followed by the class name and curly braces.
Step 2: Check each option
Only class Car { } uses the correct Dart syntax: class Car { }. Others use incorrect keywords or syntax.
Final Answer:
class Car { } -> Option D
Quick Check:
Dart class syntax = class Name { } [OK]
Hint: Use 'class' keyword followed by name and braces [OK]
Common Mistakes:
Using 'def' or 'function' instead of 'class'
Placing 'class' after the name
Missing curly braces
3. What will be the output of this Dart code?
class Dog {
String name;
Dog(this.name);
void bark() {
print('Woof! My name is $name');
}
}
void main() {
var dog = Dog('Buddy');
dog.bark();
}
medium
A. Woof! My name is Buddy
B. Woof! My name is dog
C. Error: Missing constructor
D. Nothing is printed
Solution
Step 1: Understand the class and constructor
The class Dog has a constructor that sets the name property to 'Buddy'.
Step 2: Analyze the bark method call
The bark() method prints 'Woof! My name is ' followed by the dog's name, which is 'Buddy'.
Final Answer:
Woof! My name is Buddy -> Option A
Quick Check:
dog.bark() prints name set by constructor [OK]
Hint: Constructor sets name; bark prints it [OK]
Common Mistakes:
Assuming default constructor without parameters
Forgetting string interpolation syntax
Expecting no output without main()
4. Identify the error in this Dart class definition:
class Person {
String name
int age;
Person(this.name, this.age);
}
medium
A. Missing semicolon after 'String name'
B. Constructor syntax is wrong
C. Class name should be lowercase
D. No error, code is correct
Solution
Step 1: Check property declarations
The line String name is missing a semicolon at the end, which is required in Dart.
Step 2: Verify constructor and class name
The constructor syntax is correct, and class names start with uppercase by convention, so no error there.
Final Answer:
Missing semicolon after 'String name' -> Option A
Quick Check:
Every property line needs a semicolon [OK]
Hint: Check each line ends with a semicolon [OK]
Common Mistakes:
Ignoring missing semicolons
Thinking constructor syntax is wrong
Confusing naming conventions with errors
5. You want to create a class Book that stores a title and author, and has a method describe() that returns a string like "Title by Author". Which code correctly implements this?
hard
A. class Book {
String title, author;
Book(this.title, this.author);
String describe() {
print('$title by $author');
}
}
B. class Book {
String title, author;
Book(this.title, this.author);
void describe() {
print('$title by $author');
}
}
C. class Book {
String title, author;
Book(this.title, this.author);
String describe() {
return '$title by $author';
}
}
D. class Book {
String title, author;
Book(this.title, this.author);
describe() {
print('$title by $author');
}
}
Solution
Step 1: Check method return type and behavior
The method describe() should return a string, so its return type must be String and it must use return.
Step 2: Analyze each option
class Book {
String title, author;
Book(this.title, this.author);
String describe() {
return '$title by $author';
}
} correctly returns the string. class Book {
String title, author;
Book(this.title, this.author);
void describe() {
print('$title by $author');
}
} returns void and prints instead of returning. class Book {
String title, author;
Book(this.title, this.author);
String describe() {
print('$title by $author');
}
} returns String but uses print, which returns void. class Book {
String title, author;
Book(this.title, this.author);
describe() {
print('$title by $author');
}
} omits return type and prints instead of returning a string.
Final Answer:
class Book {
String title, author;
Book(this.title, this.author);
String describe() {
return '$title by $author';
}
} -> Option C
Quick Check:
Method returns string with return keyword [OK]
Hint: Use return with correct type for methods [OK]
Common Mistakes:
Using print instead of return
Omitting return type
Returning void when string expected
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.