title: How to give child class property a different type from the superclass in Dart?
body: <p>I am in a situation where I'm working with entities and models.
  Basically my entities describe how my data should look like, and my models extends those entities and have a function toMap to transform them into a json object.</p>
  <p>So I have 2 entities, Car and Brand, and one of the Car properties is of type Brand.</p>
  <pre class="lang-dart prettyprint-override"><code>class Car {
    final String name;
    final Brand brand;
  
    const Car({
      required this.name,
      required this.brand,
    });
  }
  
  class Brand {
    final String label;
    final String color;
  
    const Brand({
      required this.label,
      required this.color,
    });
  }
  </code></pre>
  <p>Then I have 2 models CarModel and BrandModel, which extends respectively Car and Brand.</p>
  <pre class="lang-dart prettyprint-override"><code>class CarModel extends Car {
    CarModel({
      required String name,
      required BrandModel brand,
    }) : super(
            name: name,
            brand: brand,
          );
  
    Map&lt;String, dynamic&gt; toMap() {
      return {
        'name': name,
        'brand': brand,
      };
    }
  }
  
  class BrandModel extends Brand {
    BrandModel({
      required String label,
      required String color,
    }) : super(
            label: label,
            color: color,
          );
  
    Map&lt;String, dynamic&gt; toMap() {
      return {
        'label': label,
        'color': color,
      };
    }
  }
  </code></pre>
  <p>As you can see, in <code>CarModel</code>, I would like the <code>brand</code> property to have a type <code>BrandModel</code>.
  However, it takes its superclass type.</p>
  <p>So in the end, the <code>brand</code> property in my <code>CarModel</code> has a type of <code>Brand</code>.</p>
  <p>My problem is that in the toMap function, I would like to use brand.toMap like so:</p>
  <pre class="lang-dart prettyprint-override"><code>  Map&lt;String, dynamic&gt; toMap() {
      return {
        'name': name,
        'brand': brand.toMap(), // &lt;= I want to use toMap() here, so I need brand to be of type BrandModel 
      };
    }
  </code></pre>
  <p>Is there a way to force my child class to have a different type for a property inherited from the superclass?</p>