0

I have this class, trying to insert icon and maybe rotating it inside the container depending on rotationAngle.

class ReusableCardIconLayout extends StatelessWidget {
  final double? rotationAngle;

  ReusableCardIconLayout({this.rotationAngle});

  @override
  Widget build(BuildContext context) {
    return Expanded(
      flex: 2,
      child: Transform.rotate(
        angle: rotationAngle == null ? 0.0 : rotationAngle,
      ),
    );
  }
}

Where, rotationAngle is optional, and so I made it nullable.

Now, the line

angle: rotationAngle == null? 0.0 : rotationAngle,

is showing the error "The argument type 'double?' can't be assigned to the parameter type 'double'".

Why it is giving such error? I have already checked if it is null or not. And if it was, then giving default 0.0 value or else using it's value. What's wrong in this line?

Isn't there any way to use ternary operator like above and solve it?

Is giving the default parameter value only the option left?

3 Answers3

6

You can use the Null Coalescing Operator (??):

child: Transform.rotate(
  angle: rotationAngle ?? 0.0,
),

If rotationAngle != null, the rotationAngle value would be used, otherwise - 0.0.

mkobuolys
  • 4,499
  • 1
  • 11
  • 27
0

The type of rotationAngle is nullable so you should use a ! to use it.
For example:

angle: rotationAngle == null ? 0.0 : rotationAngle!
Darshan
  • 4,020
  • 2
  • 18
  • 49
0

I think you already move your project to NULLSAFE

try this one

angle: rotationAngle == null? 0.0 : rotationAngle!,
Sam Chan
  • 1,665
  • 4
  • 14