How to check string value is zero or not

655 views
Skip to first unread message

Sac123

unread,
May 1, 2020, 10:54:41 AM5/1/20
to Flutter Development (flutter-dev)
Hi,

I am completely new to flutter I have simple doubt how to check string value is zero or not in flutter using if condition.
Is there any property like isNotEmpty to check non zero value.

Thanks in advance for your help.

Souvik Dutta

unread,
May 1, 2020, 11:13:16 AM5/1/20
to Sac123, Flutter Development (flutter-dev)
Zero and null are different things. There is a way and you have almost touched it. 
String v = null;
if (v.isEmpty){
    return "true";
}else{
    return "false";
}

This is the overall syntax. The return statements can have anything you want to return. Now null is no value at all. But zero is an actuall value. Zero takes space in the memory but null doesn't. They are different.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/5d46ee19-4b86-401d-8927-b509199e8fbb%40googlegroups.com.

Graham Dickinson

unread,
May 1, 2020, 11:52:21 AM5/1/20
to Flutter Development (flutter-dev)
Don't think you can call is Empty on a null variable. Will throw an error.

Souvik Dutta

unread,
May 1, 2020, 1:14:42 PM5/1/20
to Graham Dickinson, Flutter Development (flutter-dev)
Oo didn't know that then probably type checking will help. Like using 
String v = null;
if (v.runtimetype == null){
print("null")
}


Souvik flutter dev

On Fri, May 1, 2020, 9:22 PM Graham Dickinson <dickinso...@gmail.com> wrote:
Don't think you can call is Empty on a null variable. Will throw an error.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.

Suzuki Tomohiro

unread,
May 1, 2020, 1:16:57 PM5/1/20
to Flutter Development (flutter-dev)
Yes, Flutter’s language Dart has “isNotEmpty”.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.

Danilo Costa Viana

unread,
May 1, 2020, 1:37:47 PM5/1/20
to Flutter Development (flutter-dev)
To check if a String is null simply call

String s = null;
if (s == null) {
// Do something
}

To check if the String has no characters in it do

String s = '';
if (s.isEmpty) {
// Do something
}

To do both checks at the same time do


if (s?.isEmpty != false) {
// Do something
}

A little explanation on the third part. In Dart there are some so called null safe operations. These operations work if the variable is initialized but will not be called if they are null (it's an error to call a method on a null variable)

So calling s?.isEmpty means "if s is not null, call isEmpty on it, if it's null then don't call isEmpty and just return null".

This if has three possible values. It can be true or false (if s is not null) or it can be null (if s is null).

In your case, true and null are kind of equivalent (they both mean s holds no value to you) while false means s holds some value and thus isn't empty.

Hence the comparison "if (s?.isEmpty != false)".

Reply all
Reply to author
Forward
0 new messages