有没有办法断言用户永远不会发送空文本
我有一个文本字段,然后将值保存到一个称为消息
消息的变量是一个可确定的字段字符串?
像往常
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用.trim()
Try using .trim()