Flutter Mixins : Get started with mixins

 

Flutter Mixins :

Flutter Mixins is mainly used to make use of the code in multi level hierarchy’s i.e., currently you can only extend single class and there is no option for multiple classes.

So in order to make use of the code from different classes we make use of mixins where we can extend more than 1 class and get all the properties out of it.

Mixin also help us to reduce the code by reusing the available code multiple times without repeating so that the compiler can work much faster in providing the output.

This concept will also improves the overall functionality of the code by avoiding unwanted code implementation and processing thereafter.

By making use of this concept in this tutorial we have implemented a bluetooth speaker example may go through the video below for detailed instructions.

So i suggest you to may use of the below concept and try to play with it by specifying your own requirements with mixin and try to implement them.

 

Flutter Mixins Video Tutorial:

To make this process much easier i have a added a youtube video below just go through it for detailed implementation procedure.

I have explained the process of mixins in this part of the tutorial.

 

main.dart :

I am trying to make use of simple example which we use in daily life i.e., mobile device which we make use for various functionalities i have considered a few.

Specify the functionality of the bluetooth speaker under the mixin and can use it in Bluetooth class and call in the void main as state below.

 

void main(){

  BlueToothSpeaker().functionality();
  BlueToothSpeaker().auxcable();
  BlueToothSpeaker().sdcard();
  BlueToothSpeaker().light();
}

class Speaker{

  void functionality(){
    print('I am audio output device');
  }
}

class BlueToothSpeaker extends Speaker with Torch, ExternalStorage, Cable{
  @override
  void functionality(){
    super.functionality();
    print('I can play songs wirelessly, i am portable');
  }
}

mixin Torch{

  void light(){
    print('I can be used as torch');
  }
}

mixin ExternalStorage{

  void sdcard(){
    print('I can play songs from sdcard');
  }
}

mixin Cable{

  void auxcable(){
    print('I can play songs from aux cable');
  }
}

 

If you are having any query’s in the implementation of flutter mixins do let us know in the comment section below. If you like this tutorial do like and share us for more interesting updates.

 

Show Buttons
Hide Buttons
Read previous post:
flutter_redux
Flutter getting started with redux implementation

  Flutter Redux : Flutter redux is a uni directional data flow architecture for easy development of apps.This way of...

Close