【问题标题】:How can I resolve "The argument type 'String?' can't be assigned to the parameter type 'String' " - Flutter如何解决“参数类型'字符串?'不能分配给参数类型'String'” - Flutter
【发布时间】:2021-12-10 07:17:03
【问题描述】:

enter image description hereRan 进入“参数类型'字符串?'不能在下面的代码中赋值给参数类型'String'”

静态提取文本(VisionText visionText){ 字符串文本 = '';

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;

}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    “参数类型'字符串?'不能赋值给参数类型'String'"

    当需要一个非空的String 值,但提供了一个可空的String? 时,会发生此错误。

    在这种情况下,如果您的 text 是非空的 String 并且您的 word.text 是可空的 String?,您可以这样做:

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

    这里,?. 运算符用于检查word 是否为空,然后获取text 值。 ?? 如果word?.text 为空,则取一个空字符串作为值。因此该值始终为非空值。

    您可以在此处阅读更多关于documentation 的信息。

    【讨论】:

    • 谢谢你成功了
    • @MayankShekhar 您能否将我的答案标记为已接受的答案,非常感谢! ;)
    【解决方案2】:

    你可以试试:

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

    【讨论】:

      猜你喜欢
      • 2019-09-23
      • 2021-08-02
      • 2020-08-25
      • 2021-09-04
      • 2018-04-05
      • 2021-10-18
      • 2021-10-17
      • 2021-09-30
      • 2023-04-05
      相关资源
      最近更新 更多