Thuta Learning
IntermediateProgrammingbeginner

Mixins

Relax. We'll talk through this in plain words — no textbook voice.

Mixin lets you add reusable behavior into multiple classes. In Dart, a class can only extend one parent class, but it can add multiple mixins using with.

dart
mixin Logger {
  void log(String message) {
    print('[LOG] $message');
  }
}

mixin Shareable {
  void share(String url) {
    print('Sharing: $url');
  }
}

class BlogPost with Logger, Shareable {
  final String title;
  BlogPost(this.title);
}

void main() {
  final post = BlogPost('Learn Dart');
  post.log('Post opened');
  post.share('https://thutatech.com');
}

BlogPost class picks up both Logger and Shareable behavior. Putting common utility methods in a mixin makes them easy to reuse across multiple classes.

You should see
[LOG] Post opened Sharing: https://thutatech.com

Easy traps

  • Don't cram a bunch of unrelated logic into a mixin. A mixin with one clear, single responsibility is much easier to maintain.
Mixins | Thuta Learning