<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Syed Ibrahim on Medium]]></title>
        <description><![CDATA[Stories by Syed Ibrahim on Medium]]></description>
        <link>https://medium.com/@rocksyed10?source=rss-4040cf9ae537------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6T0qeQYUKlHuhk7_KMSCQQ.jpeg</url>
            <title>Stories by Syed Ibrahim on Medium</title>
            <link>https://medium.com/@rocksyed10?source=rss-4040cf9ae537------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 09:53:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rocksyed10/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Exploring Mixins in Dart — Flutter]]></title>
            <link>https://medium.com/flutter-community/exploring-mixins-in-dart-flutter-43514cd9952b?source=rss-4040cf9ae537------2</link>
            <guid isPermaLink="false">https://medium.com/p/43514cd9952b</guid>
            <category><![CDATA[mixin]]></category>
            <category><![CDATA[inheritance]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[dart]]></category>
            <dc:creator><![CDATA[Syed Ibrahim]]></dc:creator>
            <pubDate>Fri, 08 Mar 2024 20:04:43 GMT</pubDate>
            <atom:updated>2024-11-06T11:40:19.645Z</atom:updated>
            <content:encoded><![CDATA[<h3>Mixins in Dart (Flutter) — And How it is different from Inheritance ?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7iHfjkAcz1VOlNQ38a0KfQ.jpeg" /></figure><p><strong><em>Hello all!</em></strong></p><p>Let’s talk about <strong><em>mixins</em></strong> today, with a question.</p><p>What is <strong><em>mixin</em></strong>? and how it is different from <strong><em>inheritance. </em></strong>If you know how it is different from <strong><em>inheritance</em></strong> then scroll to section 2 and check how to implement <strong><em>mixins :) </em></strong>if not we’ll break it down right here!</p><h4><strong>Section 1 : Let’s start with definition</strong></h4><blockquote>mixin is a way to reuse code by allowing classes to inherit behaviours and properties from multiple sources.</blockquote><p><strong>Right ?</strong> this is what you’ll find mostly when you search about <strong><em>mixins</em></strong>. And most probably you’ll get confused it with inheritance like me :) . So, before we start with <strong><em>mixins</em></strong> it is very important to know about is-a relationship (inheritance) and has-a relationship (composition) .</p><p><strong>So let us have some quick clarification on these concepts. have a look at this image</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*z-UiaUJuHBU3t15C1NXiEw.png" /></figure><p><strong><em>What you observe?</em></strong></p><p>From the real-time example attached for reference. You can observe here that-</p><ul><li><strong>Samsung <em>is-a</em> Mobile</strong></li><li><strong>Samsung <em>has-a</em> Camera and <em>has-a</em> Bluetooth.</strong></li></ul><p>Now check out the following definitions to get more insight.</p><p><strong><em>is-a Relationship</em></strong><em> : It can be defined as direct relationship between two class where one class (lets say Class B) is subclass of other class (lets say Class A). Which is called as </em>INHERITANCE<em>.</em></p><p>— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —</p><p><strong><em>has-a Relationship</em></strong><em>: It can be defined as an association between two classes where one class (let’s say Class A) contains an instance of another class (let’s say Class B) as one of its members. This association allows Class A to have access to the functionalities and properties of Class B, thus known as </em>COMPOSITION<strong><em>.</em></strong></p><p><strong>Finally let’s see how it looks programmatically.</strong></p><p><em>*Attention: focus on comments*</em></p><pre>//parent class<br>class Mobile {<br>}<br><br><br><br>//subclass or child class<br>//example of is-a relationship as Samsung extends Mobile<br>class Samsung extends Mobile {<br><br>//example of has-a relationship <br> Wifi wifi = Wifi();<br> Camera camera = Camera();<br><br>}</pre><p>I Hope you’re now clear about <strong>is-a</strong> and <strong>has-a</strong> relationship.</p><p><strong>Now let’s read the definition of mixin again, which I hope you already did.</strong></p><blockquote><strong>mixin</strong> is a way to reuse code by allowing classes to inherit behaviours and properties from multiple sources, “without establishing a strict hierarchy of parent child relationship which is what inheritance (is-a relationship) does”.</blockquote><p>So classes using <strong><em>mixin</em></strong> can inherit all properties and functions but cannot be termed as subclass.</p><p><strong>why????</strong></p><p>Because <strong><em>mixins</em></strong> establish a relationship similar to, but not exactly, a “has-a” relationship between a class and the functionalities provided by the <strong><em>mixin</em></strong>. It gains access to the methods and properties defined in the <strong><em>mixin</em></strong>.</p><p>However, the difference is the class itself doesn’t hold an instance of the <strong><em>mixin</em></strong>; rather, it just inherits it. There’s no strict hierarchy involved. So as it is not being bound by a strict hierarchy of parent-child relationships.</p><p><strong>This flexibility allows classes to benefit from the functionalities provided by <em>mixins</em> without forming a direct “is-a relationship” with them.</strong></p><p>Understood? If not, hang tight — we’ll dive into it in Section 2 and you’ll get it in no time!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*ETWMK2xQnq54XonmQMumIw.gif" /></figure><h4><strong>Section 2 : Implementation of mixin</strong></h4><p>let’s dive now into how we can implement them in Dart and Flutter.</p><p><strong>1.Declaring a Mixin</strong> : We declare a <strong><em>mixin</em></strong> using the <strong><em>mixin</em></strong> keyword followed by the <strong><em>mixin&#39;s</em></strong> name. Mixins can contain methods and properties.</p><pre>mixin MixinLogger {<br><br> void logMessage(String message) { <br>   print(&quot;MESSAGE: $message&quot;);<br>  }<br><br>}</pre><p><strong>2.Using Mixins :</strong> To use a <strong><em>mixin</em></strong> in a class, we use the <strong><em>with</em></strong> keyword followed by the <strong><em>mixin&#39;s</em></strong> name. This allows the class to inherit the functionalities defined in the mixin.</p><pre>class APIService with MixinLogger { <br><br>  void getPosts() { <br>    try { <br>      final response = http.get(&#39;https://www.example.com/posts&#39;); <br>     } catch (Exception e) { <br>        // Call mixin method <br>        logMessage(e.toString()); <br>     } <br>    } <br><br>  }</pre><p>However, the difference is the class itself doesn’t hold an instance of the <strong><em>mixin</em></strong>; rather, it just inherits it. There’s no strict hierarchy involved. So as it is not being bound by a strict hierarchy of parent-child relationships.</p><p><strong>This flexibility allows classes to benefit from the functionalities provided by mixins without forming a direct “is-a relationship” with them.</strong></p><p>Now let’s explore more features.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*j5nDu39KHxe3669LfJKieg.gif" /></figure><h4><strong>3. Features in mixin:</strong></h4><p><strong><em>on keyword in mixin:</em></strong></p><p>If you want to restrict your mixin to be used by only those classes which needs to subclass of your desired class then we should declare mixin class with on keyword.</p><p><strong><em>mixin</em></strong> &lt;<strong>mixinname</strong>&gt; <strong><em>on</em></strong> &lt;<strong>class_name_on_which_mixin_should_restricted</strong>&gt;</p><p><em>*Attention: focus on comments*</em></p><pre><br>mixin MixinDiscount on Product{<br><br>  performDiscountOperation(double price,double discount){<br>     //perform discount operations<br>    }<br><br>}<br><br><br>class Product{<br><br>}</pre><p>Here Discount <strong><em>mixin</em></strong> will be constrained and can only be used with class which is a subclass of product class Type.</p><pre>//It will throw compile time error that<br>//Telvision can&#39;t be mixed as it should be implement Product<br>class Television with MixinDiscount{<br><br>}</pre><pre>//it will work as it adheres the rules<br>class Television extends Product with MixinDiscount{<br><br>  void doSomeOperation(){<br>    //you can call methods directly now  <br>    performDiscountOperation(1000,10);<br>   }<br><br>}</pre><p><strong><em>inheriting multiple mixins :</em></strong></p><p>Let’s declare two mixins MixinA and MixinB with one functions each.</p><pre><br>mixin MixinA{<br><br>  void functionA(){<br>    print(&quot;function A&quot;);<br>   }  <br><br>}<br><br>mixin MixinB{<br><br>  void functionB(){<br>    print(&quot;function B&quot;);<br>  }<br><br>}</pre><pre>class Consumer with MixinA,MixinB{<br><br>}<br><br>void main(){<br>  final consumer = Consumer();<br>  //Now consumer can be able to use both functions from mixin A and B<br>  consumer.functionA();<br>  consumer.functionB();<br>}<br></pre><p><strong>What if multiple mixins having same function signatures?</strong></p><p>Dart got you covered :)</p><pre>mixin MixinA {<br>  void foo() {<br>    print(&#39;A.foo&#39;);<br>  }<br>}<br><br>mixin MixinB {<br>  void foo() {<br>    print(&#39;B.foo&#39;);<br>  }<br>}</pre><p>Now, let’s define a class C that uses both mixins A and B:</p><pre>class ClassC with MixinA, MixinB {<br>  // Class implementation...<br>}<br><br><br>void main(){<br>  final c = ClassC();<br>  c.foo(); // Output: B.foo<br>}</pre><p>In this situation, the classC gets the <strong>foo()</strong> method from both mixins <strong>A</strong> and <strong>B </strong>But Dart has to decide which one to use since there’s a conflict. It picks the <strong>foo()</strong> method from the last <strong><em>mixin</em></strong> used, which is <strong>B</strong>. So, when we make an instance of the class <strong>C</strong> and call <strong>foo()</strong>, it calls the func from mixin <strong>B</strong>.</p><p>The programming languages that support multiple inheritance have complexity for this type of scenario, which is known as the diamond problem. But for Dart Easy, right? :)</p><p>That’s all for now.</p><p>Thanks for reading !!!</p><p>My First Ever Blog ! I Hope It Helped :)</p><p>Ibrahim Syed.</p><p><a href="https://www.linkedin.com/in/ibrahimsyed7">https://www.linkedin.com/in/ibrahimsyed7</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=43514cd9952b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-community/exploring-mixins-in-dart-flutter-43514cd9952b">Exploring Mixins in Dart — Flutter</a> was originally published in <a href="https://medium.com/flutter-community">Flutter Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>