I am passing true
to a widget to calculate the border radius of a button. For some reason round
passes as null
and the ternary in the next simplified code returns 25. What am I doing wrong?
CustomButton(
height: 60.0,
round: true,
);
class CustomButton extends StatelessWidget {
CustomButton({
@required this.height,
this.round,
});
final height;
final round;
@override
Widget build(context) {
return CustomElevation(
child: RawMaterialButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(round == true ? height / 2 : 25),
),
),
),
);
}
}
Solution 1: Kohls
CustomButton(
height: 60.0,
round: true,
);
First off, round == true is precisely the same as just round. Next, you should probably add a "bool" annotation to the round var. Then also realize if that widget is rebuilt, I'm not sure round is keeping its value, unless you put it into a stateful widget's state (mention)
Second, try using null-type safe, so it prevents nullability
Third, if a parameter request double value, use double value, not integer
class CustomButton extends StatelessWidget {
CustomButton({
@required this.height,
this.round,
});
final double height;
final bool round;
@override
Widget build(context) {
return CustomElevation(
child: RawMaterialButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular((round ?? false) ? height / 2.0 : 25.0),
),
),
),
);
}
}