Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart set default value for parameter

Tags:

flutter

dart

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

like image 997
DolDurma Avatar asked Jun 06 '19 16:06

DolDurma


People also ask

How do I set default parameter value in darts?

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.

How do I set a default value in flutter?

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.

Can parameters have default values?

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 .


1 Answers

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;
  }
}
like image 153
Gunhan Avatar answered Oct 19 '22 05:10

Gunhan