Interfaces in Dart: Complete Guide to implements with Real Flutter Examples

Learn interfaces in Dart using the implements keyword. Understand how Dart supports multiple inheritance behavior, strict contracts, clean architecture, and real Flutter use cases with clear examples.

Introduction

In the previous article, we learned about Abstract Classes in Dart.
Now, let’s move one step forward and understand another powerful OOP concept — Interfaces in Dart.

Interfaces help you write flexible, scalable, and testable code, which is why they are heavily used in Flutter clean architecture, repositories, services, and APIs.

In this article, we’ll understand what interfaces are, how Dart implements them, why they are useful, and how they are used in real Flutter projects.

What Is an Interface in Dart?

Unlike some other languages, Dart does not have a separate interface keyword.

Instead:

Any class in Dart can act as an interface
by using the implements keyword.

Key characteristics of interfaces in Dart:

  • Define a strict contract

  • Force implementation of all methods and fields

  • Support multiple interfaces

  • Do not allow code reuse

Why Do We Use Interfaces?

Interfaces are used when we want strict rules.

They help to:

  • Ensure consistent structure

  • Support multiple inheritance safely

  • Build clean and modular architecture

  • Apply dependency inversion

  • Swap implementations easily

This is extremely useful in Flutter apps, especially for testing and large-scale projects.

Basic Interface Example

class Printer {
  void printData() {}  

Implementing the interface:

class OfficePrinter implements Printer {
  @override
  void printData() {
    print("Printing document");
  }  

Usage:

void main() {
  OfficePrinter p = OfficePrinter();
  p.printData();  

✔ When using implements, overriding the method is mandatory.

Interface with Multiple Methods

class RemoteControl {
  void turnOn() {}
  void turnOff() {} }

Implementation:

class TV implements RemoteControl {
  @override
  void turnOn() => print("TV ON");

  @override
  void turnOff() => print("TV OFF");  

❗ If you forget even one method, Dart throws a compile-time error.

Multiple Interfaces in Dart (Very Important)

One major advantage of interfaces is multiple inheritance support.

class Camera {
  void takePhoto() {}
}

class GPS {
  void getLocation() {}
}

class Smartphone implements Camera, GPS {
  @override
  void takePhoto() => print("Photo taken");

  @override
  void getLocation() => print("Location fetched");
}

✔ Dart allows multiple interfaces
✔ Safe alternative to multiple inheritance
✔ Very common in real projects

Interface vs Abstract Class

Feature Interface Abstract Class
Keyword implements extends
Multiple allowed ✔ Yes ❌ No
Code reuse ❌ No ✔ Yes
Override required All methods Only abstract ones
Constructor ❌ No ✔ Yes

Rule of thumb:

  • Use abstract class when you want shared code

  • Use interface when you want strict rules

Interface with Properties (Fields)

Interfaces also force implementation of fields.

class UserRepository {
  String userType = "";
  void fetchUser();
}

Implementation:

Class ApiUserRepo implements UserRepository {
  @override
  String userType = "API";

  @override
  void fetchUser() {
    print("Fetching user from API");
  }
}

✔ Both fields and methods must be implemented

Real Flutter Architecture Example

Interfaces are widely used in Flutter clean architecture.

class AuthService {
  void login(String email, String password) {}
}

Different implementations:

class FirebaseAuthService implements AuthService {
  @override
  void login(String email, String password) {
    print("Login via Firebase");
  }
}

class MockAuthService implements AuthService {
  @override
  void login(String email, String password) {
    print("Mock login for testing");
  }  

Benefits:

  • Easy to swap implementations

  • Ideal for unit testing

  • Clean and maintainable architecture

Common Mistakes

  • Expecting code reuse with implements

  • Forgetting to override all methods

  • Accidentally using implements instead of extends

  • Overusing interfaces for simple cases

Conclusion

Interfaces in Dart provide a powerful way to enforce structure and support multiple inheritance behavior without complexity.

They are essential for:

  • Clean architecture

  • Testable Flutter apps

  • Dependency inversion

  • Large-scale projects

Once you understand interfaces, concepts like mixins, repositories, and service layers become much clearer.

Share

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Angry Angry 0
Sad Sad 0
Wow Wow 0