有没有办法断言用户永远不会发送空文本

发布于 2025-01-28 00:20:03 字数 612 浏览 1 评论 0原文

我有一个文本字段,然后将值保存到一个称为消息消息的变量是一个可确定的字段字符串?像往常

if(message != null && message != '') {
  // use the message for a super secret super suspicious act
}

消息; 发送它,它接受它不是空的,我试图通过添加条件&&来解决这个问题。消息!=''这有效,但是现在,用户可以输入两个空格并发送,我为两个空间添加了另一个条件,直到我开始感到愚蠢,我无法处理所有空间,所以我尝试使用使用.isnotempty属性,但它不会让我,因为如您所知,Message是无效的,并且您不能使用.isnotempty在具有无效的潜力的字段上,只能解决message我无法做的null检查肯定会例外,

所以 最大的问题是,是否有一种方法可以确保用户不能通过发送一堆空间来确保用户在我的状况周围工作,聊天应用程序如何等待whatsapp等聊天应用程序,请确保您无法发送空间并将其作为字符串传递

I have a textfield, and i save the value to a variable called message message is a nullable field String? message; as usual, because it can be null before user enters text right?.. however when checking if it's empty, I've done this

if(message != null && message != '') {
  // use the message for a super secret super suspicious act
}

but this isn't enough for me coz when user types one space, and sends it, it accepts it as not empty, I tried to tackle this by adding the condition && message != ' ' this worked, but now, user can type two spaces and send, I added another condition for two spaces, till I started feeling stupid, I can't handle all the spaces, so I tried using an .isNotEmpty property, but it wouldn't let me, because as you know, message is nullable, and you can't use .isNotEmpty on a field that has the potential of being null, only work around is to add a null check to message which I can't do coz I know at some point message is gonna be null, it'll definitely throw an exception,

SO
the big question is, is there a way to make sure user can't send empty messages, or work around my condition by sending a bunch of spaces, how do chat apps like whatsapp and the likes of it make sure you can't send spaces and pass it as a string

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行至春深 2025-02-04 00:20:03

尝试使用.trim()

String message = "       ";
message.trim();
print(message);

Try using .trim()

String message = "       ";
message.trim();
print(message);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文