Classes in Dart

| Page Views: 15

Dart Object Oriented Programming Class Tutorial and Example.

We will explore classes in this tutorial.

Let's start by looking at various implementations to get a feeling of classes in dart.

Let's say we want to reprsent a single Employee with his details.

class Employee {
  String firstName;
  String lastName;
  String mobileNo;
  String emailId;

  Employee(this.firstName, this.lastName, this.mobileNo, this.emailId);
  Employee.fromMap(Map map) {
    firstName = map[firstName];
    lastName = map[lastName];
    mobileNo = map[mobileNo];
    emailId = map[emailId];

Here's another example:

class Name {
  String firstName;
  String lastName;
  Map<String, dynamic> toMap() {
    return {
      'firstName': firstName,
      'lastName': lastName

  String toString() => "$firstName $lastName";

Here's another example to represent a todo:

class Todo {
  int _id;
  String _title;
  String _description;
  String _date;
  int _priority;

  Todo(this._title, this._priority, this._date, [this._description]);
  Todo.withId(this._id, this._title, this._priority, this._date, [this._description]);

  int get id => _id;
  String get title => _title;
  String get description => _description;
  int get priority => _priority;
  String get date => _date;

  set title(String newTitle) {
    if (newTitle.length <= 255) {
      _title = newTitle;

  set description(String newDescription) {
    if (newDescription.length <= 255) {
      _description = newDescription;

  set priority(int newPriority) {
    if (newPriority >= 0 && newPriority <= 3) {
      _priority = newPriority;

  set date(String newDate) {
    _date = newDate;

  Map<String, dynamic> toMap(){
    var map = Map<String, dynamic>();
    map["title"] = _title;
    map["description"] = _description;
    map["priority"] = _priority;
    map["date"] = _date;
    if(_id != null){
      map["id"] = _id;
    return map;

  Todo.fromObject(dynamic o){
    this._id = o["id"];
    this._title = o["title"];
    this._description = o["description"];
    this._priority = o["priority"];
    this._date = o["date"];


You create an abstract class using the abstract keyword:

abstract class AuthStateListener {
  void onAuthStateChanged(AuthState state);

NB/= More examples and explanations coming soon.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:


What do You Think

Previous Post Next Post