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