PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, December 13, 2022

[FIXED] How to do null/empty check of a String inside a TextFormField validator function in dart/flutter?

 December 13, 2022     dart, dart-null-safety, flutter, null, syntax     No comments   

Issue

Is there a shorter way to check if a string is either null or empty? Do I need to write every time 2 conditions inside if to check it?

validator: (value) {
    if (value == null || value.isEmpty){
        //....
    }
}

In python we can do if value and it works. In javascript we could do if(!value).


Solution

As from my understanding, the TextEditingController use TextEditingValue.empty which is providing empty string initially. I am not able to get null exception.

I think we can use ! while we are sure the value is empty, not null.

validator: (value) {
  if (value!.isEmpty) return "Empty";

I always prefer checking null first, for nullable data type.



Answered By - Yeasin Sheikh
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing