4

Here I want to create an empty constructor to print empty constructor is called message but it shows try adding initializer error.

class ReusableCard extends StatelessWidget {
  final Color color;

  ReusableCard(){    
print('empty const is called');   
 }

  ReusableCard(this.color);

  @override   Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(15.0),
      //TODO:use decorator only if colors are added inside in it.

      decoration: BoxDecoration(
        color: Color(0xFF1D1E20),
        borderRadius: BorderRadius.circular(10.0),
      ),
      // width: 100,
      height: 100,
    );   
}
}
Bala
  • 137
  • 2
  • 10
  • This isn't legal on two levels. First, you have two constructors with the same name. Second, the first of those (not that the order matters) does not initialize the color member. You must fix both of those, mostly by just deleting the first one. – Randal Schwartz Jun 08 '21 at 17:10

3 Answers3

0
class ReusableCard extends StatelessWidget {
  final Color color;

  ReusableCard():
     this.color = const Color(0xFF1D1E20)
     {    
       print('empty const is called');   
     }

  ....
}


Dali Hamza
  • 568
  • 1
  • 4
  • 8
0

Since your color variable is final you have to initialize it on your empty constructor.

class ReusableCard extends StatelessWidget {
  final Color color;

  ReusableCard() {
    this.color = const Colors.blue;
    print('empty const is called');
  }

  ReusableCard(this.color);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(15.0),
      //TODO:use decorator only if colors are added inside in it.

      decoration: BoxDecoration(
        color: Color(0xFF1D1E20),
        borderRadius: BorderRadius.circular(10.0),
      ),
      // width: 100,
      height: 100,
    );
  }
}
quoci
  • 2,940
  • 1
  • 9
  • 21
  • 1
    `final` members (that aren't also `late`) must be initialized by an initializer list or by field (inline) initializers before the constructor body executes. See https://stackoverflow.com/a/64548861/ – jamesdlin Jun 07 '21 at 11:48
  • Ah didn't know. Thank you – quoci Jun 07 '21 at 12:19
0

Repeating the default constructor is not allowed in dart so you need to use named constructors as follows:

ReusableCard.empty() {
      print('empty');
   }

see your class:

class ReusableCard extends StatelessWidget {
     final Color color;

     ReusableCard.empty() {
          print('empty');
       }
     ReusableCard(this.color);

     @override   Widget build(BuildContext context) {
       return Container(
         margin: EdgeInsets.all(15.0),
        //TODO:use decorator only if colors are added inside in it.

         decoration: BoxDecoration(
          color: Color(0xFF1D1E20),
          borderRadius: BorderRadius.circular(10.0),
          ),
         // width: 100,
         height: 100,
         );   
         }
        }
Addow
  • 104
  • 1
  • 2
  • 7
  • Named constructors are not relevant to this problem. – jamesdlin Jun 07 '21 at 21:38
  • 1
    If you want to use more than one constructor in dart you should use named or factory constructors.Repeating the default constructor is not allowed, so please check the dart docs. – Addow Jun 08 '21 at 08:22
  • True, but that's a separate problem from the one from the error message. – jamesdlin Jun 08 '21 at 08:59
  • 2
    Thanks, but the one who asked the question needs help, and liked to tell him what is possible. – Addow Jun 08 '21 at 09:06