Home > Net >  How can I resolve "The argument type 'String?' can't be assigned to the paramete
How can I resolve "The argument type 'String?' can't be assigned to the paramete

Time:10-25

enter image description hereRan into "The argument type 'String?' can't be assigned to the parameter type 'String'" on the following code

static extractText(VisionText visionText) { String text = '';

for (TextBlock block in visionText.blocks) {
  for (TextLine line in block.lines) {
    for (TextElement word in line.elements) {
      text = text   word.text   ' ';
    }
    text = text   '\n';
  }
}

return text;

}

CodePudding user response:

you can try :

text = text   (word?.text ?? '' )   ' ';

CodePudding user response:

"The argument type 'String?' can't be assigned to the parameter type 'String'"

This error happens when a non-null String value is expected, but a nullable String? is provided instead.

In this case, if your text is a non-null String and your word.text is a nullable String?, you can do this:

text  = (word?.text ?? '')   ' ';

Here, the ?. operator is for checking if word is not null, then get the text value. ?? is in case word?.text is null, then take an empty string as the value. Thus the value is always a non-null one.

You can read more on the documentation here.

  • Related