Lorenzo Cutrupi
Lorenzo Cutrupi

Reputation: 720

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

I have copied this code from github but I get errors:

BubbleIndicatorPainter(
  {this.dxTarget = 125.0,
    this.dxEntry = 25.0,
    this.radius = 21.0,
    this.dy = 25.0,
    this.pageController})
  : super(repaint: pageController) {
painter = Paint()
  ..color = CustomTheme.white
  ..style = PaintingStyle.fill;}

In particular, BubbleIndicatorPainter gets:

Non-nullable instance field 'painter' must be initialized.

and pageController gets:

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

I think they are related but I don't know how to solve, and adding required to this.pageController didn't solve the problem. Thanks for the answers

Upvotes: 2

Views: 780

Answers (1)

Pat9RB
Pat9RB

Reputation: 650

Without seeing the rest of your code - are you passing BubbleIndicatorPainter() a valid PageController() object?

It looks like your source is from here?

If you're compiling with null-safety a variable that can be null will have a declaration with a "?" suffix. From the link above, pageController is defined as non-nullable.

 final PageController pageController;

If it could be null the declaration would be:

final PageController? pageController;

Looking at build() in the linked source, pageController.position is accessed without a null check, so you can't have a null pageController.

The 'default' in the error is a default value in the declaration, which is 'implicitly' null if not provided. For example:

class MyClass {
    Color color;
    MyClass({this.color = Colors.white});
}

The "implicit default" of "color" is "Colors.white" - the value that gets assigned if you don't provide one to MyClass(), i.e.:

MyClass myclass = MyClass(color: Colors.blue); //myclass.color will be Colors.blue
MyClass myclass = MyClass() // myclass.color will be Colors.white

If instead MyClass was:

class MyClass {
    Color? color;
    MyClass({this.color});
}

The implicit default value of color would be 'null' because no default is provided (which is allowed because it's declared with a "?" suffix). I.e.:

MyClass myclass = MyClass(color: Colors.blue); //myclass.color will be Colors.blue
MyClass myclass = MyClass() // myclass.color will be null

Looking at BubbleIndicatorPainter() it doesn't provide a default value for this.pageController, which means the implicit default is 'null' (which isn't allowed because it doesn't have a "?" after the type.)

Upvotes: 2

Related Questions