in Flutter
framework i'm trying to set default value for parameters as borderRadius
, in this sample how can i implementing that? i get Default values of an optional parameter must be constant
error when i try to set that, for example:
class SimpleRoundButton extends StatelessWidget {
final BorderRadius borderRadius;
SimpleRoundButton({
}):this.borderRadius = BorderRadius.circular(30.0);
}
class SimpleRoundButton extends StatelessWidget {
final BorderRadius borderRadius= BorderRadius.circular(30.0);
SimpleRoundButton({
this.borderRadius,
});
}
class SimpleRoundButton extends StatelessWidget {
final BorderRadius borderRadius;
SimpleRoundButton({
this.borderRadius= BorderRadius.circular(30.0)
});
}
all of this samples are incorect
In Dart, we can assign default parameter values in a function definition, so when we call that function with no argument passed for a parameter then its default value assigned.
Default function parameters allow formal parameters to be initialized with default values if no value is passed. If you forget the curly brackets, you'll see the following error message: Error: Non-optional parameters can't have a default value. Try removing the default value or making the parameter optional.
In JavaScript, a parameter has a default value of undefined. It means that if you don't pass the arguments into the function, its parameters will have the default values of undefined .
BorderRadius.circular()
is not a const function so you cannot use it as a default value.
To be able to set the const circular border you can use BorderRadius.all
function which is const like below:
class SimpleRoundButton extends StatelessWidget {
final BorderRadius borderRadius;
SimpleRoundButton({
this.borderRadius: const BorderRadius.all(Radius.circular(30.0))
});
@override
Widget build(BuildContext context) {
return null;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With