dart

How can I initialize super class variables in the Dart language?


Simply, I have two classes, a child and a parent class. I am new to the Dart language. All I need is to assign super class properties from a child class.

This is the super class structure:

class Trip {
  final int id;
  final String title;
  final double price;
  Trip({this.id, this.title, this.price});
}

And this is the child class:

class FullTrip extends Trip {
  final String data;
  FullTrip({this.data}) : super(id:id, title:title, price:price);
}

Sure this is not working at all.

How can I initialize the instance from FullTrip and pass a variable for FullTrip and Trip (super class)?


Solution

  • You need to repeat the parameters in the subclass.

    class FullTrip extends Trip{
      final String data;
      FullTrip({this.data, int id, String title, double price}) : super(id:id,title:title,price:price);
    }
    

    There are discussions about reducing such boilerplate for constructors, but nothing is decided yet as far as I know.