Book Image

DART Cookbook

By : Ivo Balbaert
Book Image

DART Cookbook

By: Ivo Balbaert

Overview of this book

Table of Contents (18 chapters)
Dart Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using mixins


In Dart, just like in Ruby, your classes can use mixins to assign a certain behavior to your class. Say an object must be able to store itself, so its class mixes in a class called Persistable that defines save() and load() methods. From then on, the original class can freely use the mixed-in methods. The mechanism is not used for specialized subclassing or is-a relationships, so it doesn't use inheritance. This is good because Dart uses a single inheritance, so you want to choose your unique direct superclass with care.

How to do it...

  • Look at the mixins project; the Embrace class from the previous recipe needs to persist itself, so it mixes with the abstract class Persistable, thereby injecting the save and load behavior. Then, we can apply the save() method to the embr object, thereby executing the code of the mixin as follows:

    void main() {
      var embr = new Embrace(5);
  • Using the mixins methods, as shown in the following code:

      print(embr.save(embr.strength));
      print(embr is...