输入图像描述到"参数类型'String ' ?'不能分配给参数类型'String'"下面的代码
static extractText(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;
}
"参数类型'String ' ?'不能分配给参数类型'String'">
当期望一个非空的String
值,但提供了一个可空的String?
时,会发生此错误。
在这种情况下,如果你的text
是一个非空的String
和你的word.text
是一个可空的String?
,你可以这样做:
text += (word?.text ?? '') + ' ';
这里,?.
运算符用于检查word
是否为空,然后获取text
的值。??
在word?.text
为null的情况下,则取空字符串作为值。因此,该值始终是非空值。
你可以在这里阅读更多的文档。
你可以试试:
text = text + (word?.text ?? '' ) + ' ';