Home > Back-end >  The parameter 'padding' can't have a value of 'null' because of its type, b
The parameter 'padding' can't have a value of 'null' because of its type, b

Time:05-01

hope u guys doing well..

so, Earlier i'm Following Flutter Tutorial On Youtube, and looks like there's no problems with his BorderBox Class, but mine is different, it have 3 problems cause of padding, width, height, are null.

class BorderBox extends StatelessWidget {

  final Widget child;
  final EdgeInsets padding;
  final double width, height;

  const BorderBox({Key? key,
    this.padding,
    this.width,
    this.height,
    required this.child}) : super(key: key);

with the error message below

The parameter 'padding' can't have a value of 'null' because of its type, but the implicit default value is 'null'.

The parameter 'width' can't have a value of 'null' because of its type, but the implicit default value is 'null'.

The parameter 'height' can't have a value of 'null' because of its type, but the implicit default value is 'null'.

is there a way too bypass the null Parameters?? also this is the video i followed the tutorial on youtube Here

Thank You, and have a good day...

CodePudding user response:

Add required in the constructor for non-nullable values like so:

  const BorderBox({
    Key? key,
    required this.padding,
    required this.width,
    required this.height,
    required this.child,
   }) : super(key: key);

However, if you don't want them to be required, you can make them nullable like so:

class BorderBox extends StatelessWidget {

  final Widget child;
  final EdgeInsets? padding;
  final double? width, height;

  const BorderBox({
    Key? key,
    this.padding,
    this.width,
    this.height,
    required this.child,
  }) : super(key: key);

The ? means that the variable can be null, which gives you the ability to not make it required.

You can read more on null safety here: https://dart.dev/null-safety

  • Related