I would like to create different objects by calling constructors that have different number of parameters. How can I achieve this in Dart?
class A{
  String b,c,d;
  A(this.b,this.c)
  A(this.b,this.c,this.d)
}
                See Constructor section of Tour of Dart.
Basically Dart doesn't support methods/constructor overloading. However Dart allows named constructors and optional parameters.
In your case you could have:
class A{
  String b,c,d;
  /// with d optional
  A(this.b, this.c, [this.d]);
  /// named constructor with only b and c
  A.c1(this.b, this.c);
  /// named constructor with b c and d
  A.c2(this.b, this.c, this.d);
}
                        You can use factory constructors
class A{
  String b,c,d;
  A(this.b,this.c,this.d)
  
  factory A.fromBC(String b, String c) => A(b, c, "");
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With