Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cant define a const constructor for a class with non final fields Flutter

Tags:

flutter

I am trying to render a widget with flutter and I am getting the following errors:

"can't define a const constructor for a class with non final fields"

"constant constructor can't call non-constant super constructor of State"

"The name parameter 'Key' ins't defined"

The code with this errors is the following:

class ContainerButton extends StatefulWidget {
  @override
  ContainerButtonState createState() => ContainerButtonState();
}

class ContainerButtonState extends State<ContainerButton> {
  final ButtonType buttonType;
  const CustomButton({Key key, this.buttonType}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(21),
      color: Color(0xfff4f5f9),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          Flexible(
            child: CustomButton(buttonType: ButtonType.download),
          ),
          Flexible(
            child: CustomButton(buttonType: ButtonType.share),
          ),
          Flexible(
            child: CustomButton(buttonType: ButtonType.problem),
          ),
        ],
      ),
    );
  }
}

I would appreciate any hint. Thank you,

like image 489
dosytres Avatar asked Aug 20 '20 10:08

dosytres


1 Answers

According to error, just remove the const keyword from Constructor and you are good to go. The Following code should remove the error :

class ContainerButtonState extends State<ContainerButton> {
  final ButtonType buttonType;
  CustomButton({Key key, this.buttonType}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(21),
      color: Color(0xfff4f5f9),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          Flexible(
            child: CustomButton(buttonType: ButtonType.download),
          ),
          Flexible(
            child: CustomButton(buttonType: ButtonType.share),
          ),
          Flexible(
            child: CustomButton(buttonType: ButtonType.problem),
          ),
        ],
      ),
    );
  }
}
like image 127
Jay Dangar Avatar answered Sep 21 '22 22:09

Jay Dangar