bihire boris
bihire boris

Reputation: 1662

how to create a constructor in flutter with null safety

This got a bit tricky for me. since we can no longer pass null values in contructor I had this

class LeadingButton extends StatelessWidget {
  final IconData icon;
  final Color color;
  final Color iconColor;
  final double iconSize;
  final double size;
  final Color backGroundColor;
  final VoidCallback onPressed;
  final double padding;
  final EdgeInsets margin;
  final double rotate;

  LeadingButton({
      @required this.icon,
      this.color = Colors.transparent,
      @required this.onPressed,
      this.size = 20,
      this.backGroundColor = Colors.transparent,
      this.iconSize = 20,
      this.iconColor = darkColor,
      this.rotate = 0,
      this.padding = 0,
      this.margin = const EdgeInsets.all(10)});
...

I am getting an error on icon and onpressed function. although the documentation is saying

The parameter 'onPressed' & 'icon' can't have a value of 'null' because of its type, but the implicit default value is 'null'. Try adding either an explicit non-'null' default value or the 'required' modifier.

I guess I am missing some thing, kindly mind to share

Upvotes: 0

Views: 5129

Answers (2)

Filippo Mazza
Filippo Mazza

Reputation: 4367

Not sure, but it could be that this is due to the recent change of the keyword. Did you try the required keyword instead?

https://dart.dev/null-safety/faq#how-does-required-compare-to-the-new-required-keyword

Upvotes: 2

Alex Radzishevsky
Alex Radzishevsky

Reputation: 3768

I think that LeadingButton's onPressed type needs to be changed to VoidCallback? so it can accept null values.

Upvotes: 1

Related Questions