Default values of an optional parameter must be constant

Try

enum MyEnum { a, b }

class ClassA {
  final MyEnum myEnum;
  ClassA({this.myEnum});
}

class ClassB {
  final ClassA classA;
  ClassB({this.classA}); // ClassA expression is underlined with red
}

no need for ‘=’ operator. It will automatically assign the value when you will pass it to the constructor.

Use the ‘=’ operator only when you need to pass a default value to your variables hence, making them optional parameters.

Edit

enum MyEnum { a, b }

class ClassA {
  final MyEnum myEnum;
  const ClassA({this.myEnum = MyEnum.a});
}

class ClassB {
  final ClassA classA;
  ClassB({this.classA = const classA()}); // ClassA expression is underlined with red
}

This is the only way i could find to achieve what you want, the constructor should be default

This is called a canonicalized constructor.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)