Skip to main content ppwriters

#30DaysMasterFlutter

Day 7

Dart Classes

OOP in Dart lets you create objects that have data and functions. You make a blueprint called a class, then create instances of that class with their own data and functions. This makes your code more organized and reusable.

Project - Article Data Source

  • Create an abstract class called ArticleDataSource with method getArticle(String id), and deleteArticle(String id).
  • Create a class called ArticleLocalDataSource that implements the ArticleDataSource
  • Implement each method and print operation, source, and the provided id. For example, calling getArticle('dart-today-and-tomorrow') should print Get Local dart-today-and-tomorrow
  • Add a factory constructor to ArticleLocalDataSource and return the object of ArticleLocalDataSource.
  • Instantiate ArticleLocalDataSource using the factory constructor and call each methods on the object.
  • Create and implement ArticleAPIDataSource exactly like ArticleLocalDataSource but print Get API dart-today-and-tomorrow instead of Get Local dart-today-and-tomorrow.

By the end of this day, you should have a good understanding of how to create classes and objects in Dart, and how to call their methods.

Tips

class Person {
  String name;
  int age;
  
  Person(this.name, this.age);
  
  void sayHello() {
    print('Hello, my name is $name and I am $age years old.');
  }
}

This creates a Person class with two properties (name and age), a constructor that initializes these properties, and a sayHello method that prints a message to the console.

  • Instantiate the class and call its methods. For example:
void main() {
  Person person = Person('Alice', 25);
  person.sayHello(); // prints "Hello, my name is Alice and I am 25 years old." to the console
}

This creates a new Person object with the name “Alice” and age 25, and calls its sayHello method.

  • Experiment with different types of classes, such as classes with private properties and methods, or classes that inherit from other classes.
class Animal {
  String _name;
  
  Animal(this._name);
  
  void speak() {
    print('My name is $_name.');
  }
}

class Cat extends Animal {
  Cat(String name) : super(name);
  
  void meow() {
    print('Meow!');
  }
}

void main() {
  Cat cat = Cat('Whiskers');
  cat.speak(); // prints "My name is Whiskers." to the console
  cat.meow(); // prints "Meow!" to the console
}

This creates an Animal class with a private _name property and a speak method, and a Cat class that extends Animal and adds a meow method.

Resources

Enjoying? Tell your friends.

#30DaysMasterFlutter

Learn the basics of Flutter and Dart in 30 days! We provide a roadmap, resources, and project ideas to help you along your journey.

back to 30 Days to Master Flutter

Join our community on Discord to connect with fellow learners, share your progress, and get help with any questions you may have throughout the #30DaysMasterFlutter challenge. Join now and get started on your journey to mastering Dart and Flutter!

join now