【问题标题】:error :A non-null String must be provided to a Text widget错误:必须向文本小部件提供非空字符串
【发布时间】:2021-10-11 20:50:27
【问题描述】:

我在运行代码时遇到问题。我看到这个错误:

Text(product.description,
          style: TextStyle(
              color: Colors.black,
          ),

和错误:

"The following assertion was thrown building DescriptionPage(dirty):
 A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
 Failed assertion: line 285 pos 10: 'data != null'"

【问题讨论】:

标签: flutter


【解决方案1】:

如果您的文本为空或为空,请尝试以下代码

Text(product.description ?? "")

【讨论】:

    【解决方案2】:
    Text((product!=null && product.description!=null)?product.description:'',
              style: TextStyle(
                  color: Colors.black,
              ),
    

    【讨论】:

      【解决方案3】:

      这个问题是由product.description 引起的。它似乎具有null 值。改用这个:

      Text(product?.description??"default text",
                style: TextStyle(
                    color: Colors.black,
                ),
      

      【讨论】:

      • product.description 不应为空或 null。当它打印在控制台上时,它不是空的。我想在文本小部件中显示 product.description 的值
      • 我知道您想要什么,但是您提供给 TextWidget 的变量现在为空!似乎您在某处更改了它或使用空值重新定义了它!分享您的完整代码,以便我查看。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 2021-07-19
      • 2020-04-14
      • 2021-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多