Home > other >  The parameter 'buttonText' can't have a value of 'null' because of its type
The parameter 'buttonText' can't have a value of 'null' because of its type

Time:04-19

I am trying to compile a calculator app, from a flutter template. I'm getting this error.

lib/buttons.dart:13:46: Error: The parameter 'buttonText' can't have a value of 'null' 
because of its type 'String', but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
MyButton({this.color, this.textColor, this.buttonText, this.buttontapped});
                                         ^^^^^^^^^^

So now I'm trying to add a value for the button text, but I'm having issues. This is what I tried, the last line is what I added, the rest were there before.

// declaring variables
final color;
final textColor;
final String buttonText;
final buttontapped;
buttonText = Whatever,

CodePudding user response:

You need to add the required statement to your class contstructor.

    //Constructor
    MyButton({this.color, this.textColor, required this.buttonText, this.buttontapped})

Or provide your default value.

//Constructor
    MyButton({this.color, this.textColor,  this.buttonText = 'what ever', this.buttontapped})

Or make your field optional, I don't think you want this!

final color;
final textColor;
String? buttonText;
final buttonTapped;

CodePudding user response:

buttonText is a string so Whatever needs to be wrapped in ''

ie

final color;
final textColor;
final buttonText;
final buttonTapped;

then

myButton({this.buttonText = 'whatever'})

or dont declare it at all until it is used

myButton({required this.buttonText})

and remove the separate declaration of it.

  • Related