【问题标题】:'package:flutter/src/widgets/text.dart': Failed assertion: line 378 pos 10: 'data != null' A non-null String must be provided to a Text widget'package:flutter/src/widgets/text.dart':断言失败:第 378 行 pos 10:'data != null' 必须向 Text 小部件提供非 null 字符串
【发布时间】:2022-01-23 18:57:33
【问题描述】:

您好,每次我在 Flutter 应用程序上单击通知详细信息时,都会出现此错误消息并变红,您可以帮我解决此错误。

请有人帮我在这里找不到解决方案

更新:我不确定这是否是解决问题的正确代码

        Align(
          alignment: Alignment.centerRight,
          child: IconButton(
            icon: Icon(Icons.close),
            onPressed: () => Navigator.of(context).pop(),
          ),
        ),

        Container(
          height: 150, width: MediaQuery.of(context).size.width,
          margin: EdgeInsets.symmetric(horizontal: Dimensions.PADDING_SIZE_LARGE),
          decoration: BoxDecoration(borderRadius: BorderRadius.circular(Dimensions.RADIUS_SMALL), color: Theme.of(context).primaryColor.withOpacity(0.20)),
          child: ClipRRect(
            borderRadius: BorderRadius.circular(Dimensions.RADIUS_SMALL),
            child: FadeInImage.assetNetwork(
              placeholder: Images.placeholder,
              image: '${Get.find<SplashController>().configModel.baseUrls.notificationImageUrl}/${notificationModel.image}',
              height: 150, width: MediaQuery.of(context).size.width, fit: BoxFit.cover,
              imageErrorBuilder: (c, o, s) => Image.asset(
                Images.placeholder, height: 150,
                width: MediaQuery.of(context).size.width, fit: BoxFit.cover,
              ),
            ),
          ),
        ),
        SizedBox(height: Dimensions.PADDING_SIZE_LARGE),

        Padding(
          padding: EdgeInsets.symmetric(horizontal: Dimensions.PADDING_SIZE_LARGE),
          child: Text(
            notificationModel.title,
            textAlign: TextAlign.center,
            style: robotoMedium.copyWith(color: Theme.of(context).primaryColor, fontSize: Dimensions.FONT_SIZE_LARGE),
              ),
        ),

        Padding(
          padding: EdgeInsets.fromLTRB(20, 10, 20, 20),
          child: Text(
            notificationModel.description,
            textAlign: TextAlign.center,
            style: robotoRegular.copyWith(color: Theme.of(context).disabledColor),
          ),
        ),

      ],
    ),
  ),
);

} }

当我将代码更新为 Text(someVariable ?? 'Default text if varible is null') mobile screenshot

我的 text.dart 错误位置

 assert(
      data != null,
     'A non-null String must be provided to a Text widget.',
   ),
   textSpan = null,
   super(key: key);

【问题讨论】:

  • 您正在某处使用Text 小部件,该小部件接受一些值为null 的变量。
  • 添加一些代码,以便我们可以看到您在做什么以及可能出现的问题。
  • 您好,感谢您的回复,我在哪里可以获得与此问题相关的代码,是否在 main.dart 文件中?

标签: flutter


【解决方案1】:

我认为您有 Text 小部件,您在其中传递带有文本的变量,在这种情况下为 null。 所以基本上你需要在 Text 小部件中使用它之前检查该变量。

您可以像这样检查 null 安全:

Text(someVariable ?? 'Default text if varible is null')

【讨论】:

  • 您好,谢谢您的回复,我完全是新手,您能告诉我在哪里可以找到这条线吗?
【解决方案2】:

你可以使用精灵运算符.. 以下代码是您代码的一部分

Padding(
          padding: EdgeInsets.symmetric(horizontal: Dimensions.PADDING_SIZE_LARGE),
          child: Text(
            notificationModel.title ?? "Text value is null",
            textAlign: TextAlign.center,
            style: robotoMedium.copyWith(color: Theme.of(context).primaryColor, fontSize: Dimensions.FONT_SIZE_LARGE),
              ),
        ),

【讨论】:

  • 嗨,是的,它修复了错误,但现在可能通知下只有一个图像持有者和一个文本(文本值为空)?应该是订单通知
  • get control 为什么 notificationModel.title 是空的?你的数据库控制器可能有错误
  • 如果这是问题,我可以知道如何解决它吗?
  • 其实当然是在数据发布(保存数据库)函数中使用 print("$notificationModel.title") 函数,在控制台打印输出。如果完成数据库post函数时输出null,那么你的post函数不正确或检查数据库中的数据表
  • 非常感谢您提供的信息。会做这个。
猜你喜欢
  • 2023-03-24
  • 2020-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-26
  • 2019-12-09
  • 2020-11-21
  • 2020-11-18
相关资源
最近更新 更多