99

This is my code for true on everything but empty string, null and false:

if (routeinfo["no_route"] == "" || routeinfo["no_route"] == null || routeinfo["no_route"] == false) {
    // do sth ...
}

This is my code for true on everything but empty string, null, false or zero:

if (routeinfo["no_route"] == "" || routeinfo["no_route"] == null || routeinfo["no_route"] == false || routeinfo["no_route"] == 0) {
    // do sth...
}

How can I write this shorter in Dart? Or is it not possible?

Blackbam
  • 17,496
  • 26
  • 97
  • 150
  • 1
    Is there a typo in your question? By `routeinfo["no_route"] != false` you mean `routeinfo["no_route"] == false`, right? – Harry Terkelsen Feb 24 '17 at 19:21
  • Yeah right edited. Bit tired its late ;-) – Blackbam Feb 24 '17 at 19:22
  • 2
    You could design your application in a way that a key will only be there if there is a truthy value to it, so you can just check for the presence of the key and trust it. Maybe it's impossible design it that way (depending on the context), but it's worth trying. – Gabriel Feb 24 '17 at 19:35

10 Answers10

210

If your requirement was simply empty or null (like mine when I saw this title in a search result), you can use Dart's safe navigation operator to make it a bit more terse:

if (routeinfo["no_route"]?.isEmpty ?? true) {
  // 
}

Where

  • isEmpty checks for an empty String, but if routeinfo is null you can't call isEmpty on null, so we check for null with
  • ?. safe navigation operator which will only call isEmpty when the object is not null and produce null otherwise. So we just need to check for null with
  • ?? null coalescing operator

If your map is a nullable type then you have to safely navigate that:

if (routeinfo?["no_route"]?.isEmpty ?? true) {
  //
}
Jannie Theunissen
  • 28,256
  • 21
  • 100
  • 127
  • Interesting syntax Can you explain it a bit more in detail what role of the questionmarks is? The classical elvis is `?:` right? – Blackbam Oct 23 '18 at 14:19
  • @Blackbam - updated the answer with some explanations – Jannie Theunissen Oct 24 '18 at 09:43
  • 16
    Another nice way to write this: `if ((value ?? '') == '') { ... }` – Lynn Apr 15 '19 at 23:36
  • best answer. $0.02 - this is the way it should be done unless there's a specific reason not to – Gene Bo Sep 27 '20 at 22:22
  • 1.) The expression `myString?.isEmpty ?? true` is a "nullable expression", and if your `if` statement has more than one condition (like the OP's example), each nullable expression must be wrapped in parenthesis, which is annoying and decreases readability. 2.) Syntactic sugar is great, but most of us write code that others also have to read, use, maintain, etc. Unless some drastic performance increase would be recognized with a more complex solution, it is usually best to go with the simple solution that everyone will understand. In this case that is `myString != null && myString.isNotEmpty`. – ubiquibacon Nov 29 '22 at 21:43
  • @ubiquibacon agreed. As I said, if your requirement is simply empty or null then this answer is good. If, like the OP your requirement is more complex, there are better answers here. Including your suggestion. – Jannie Theunissen Dec 01 '22 at 06:49
72

You could do

if (["", null, false, 0].contains(routeinfo["no_route"])) {
  // do sth
}
Harry Terkelsen
  • 2,554
  • 13
  • 11
  • 1
    Getting error: type 'Null' is not a subtype of type 'String' – Kamlesh Jun 05 '21 at 03:35
  • @Kamlesh - lowercase null? not in quotes? (i.e. null, not Null or 'null') – Curt Eckhart Jun 06 '21 at 12:45
  • Kindly suggest me what is difference null, Null and NULL? Thanks – Kamlesh Jun 06 '21 at 13:40
  • Creating a mutable data structure for an empty string check?! In general, this is very bad practice, but is especially bad practice when you consider that Dart's primary function in this world is as a language for mobile devices (i.e. embedded systems) with limited memory and processing power. Please don't do this! – ubiquibacon Nov 29 '22 at 21:49
42

Late 2020 Update

Summary

  • This answer holds true, except for isNull and isNotNull. They no longer provide type promotion when Null Safety is introduced in dart/flutter in the future.
  • Other helpers like isNullOrEmpty do not provide type promotion, as they are in a different (sub-)scope compared to callsite.
  • My personal opinion, is that you can drop isNull and isNotNull but keep other helpers as you shouldn't expect them to do type promotion for you.

Explanation

Demonstration

Here's a demonstration why encapsulation/helper-getter of isNull (== null) and isNotNull (!= null) is a very big problem:

// Promotion works
int definitelyInt(int? aNullableInt) {
  if (aNullableInt == null) { // Promote variable `aNullableInt` of Nullable type `int?` to Non-Nullable type `int`
    return 0;
  }
  return aNullableInt; // Can't be null! This variable is promoted to non-nullable type `int`
}

When "Null Safety" is shipped in the dart release you are using, the type promotion in above code works! HOWEVER:

// Promotion does NOT work!!!
int definitelyInt(int? aNullableInt) {
  if (aNullableInt.isNull) { // does NOT promote variable `aNullableInt` of Nullable type `int?`
    return 0;
  }
  return aNullableInt; // This variable is still of type `int?`!!!
}

The above doesn't work, because the null check == null and != null are encapsulated in a sub-scope (different stack frame) and not inferred to have this "promotion" effect within definitelyInt scope.



Early 2020 Update

Here's a modified version using getters instead of instance/class methods and covering whitespaces, isNull, and isNotNull.

Second Update

It also accounts for empty lists and maps now!

Third Update

Added private getters for safety and modularity/maintainability.

Fourth Update

Updated the answer to fix a particular problem with Map, it's not correctly being handled when empty! Because Map is not an Iterable. Solved this issue by introducing _isMapObjectEmpty U

Solution

extension TextUtilsStringExtension on String {
  /// Returns true if string is:
  /// - null
  /// - empty
  /// - whitespace string.
  ///
  /// Characters considered "whitespace" are listed [here](https://stackoverflow.com/a/59826129/10830091).
  bool get isNullEmptyOrWhitespace =>
      this == null || this.isEmpty || this.trim().isEmpty;
}

/// - [isNullOrEmpty], [isNullEmptyOrFalse], [isNullEmptyZeroOrFalse] are from [this StackOverflow answer](https://stackoverflow.com/a/59826129/10830091)
extension GeneralUtilsObjectExtension on Object {
  /// Returns true if object is:
  /// - null `Object`
  bool get isNull => this == null;

  /// Returns true if object is NOT:
  /// - null `Object`
  bool get isNotNull => this != null;

  /// Returns true if object is:
  /// - null `Object`
  /// - empty `String`s
  /// - empty `Iterable` (list, set, ...)
  /// - empty `Map`
  bool get isNullOrEmpty =>
      isNull ||
      _isStringObjectEmpty ||
      _isIterableObjectEmpty ||
      _isMapObjectEmpty;

  /// Returns true if object is:
  /// - null `Object`
  /// - empty `String`
  /// - empty `Iterable` (list, map, set, ...)
  /// - false `bool`
  bool get isNullEmptyOrFalse =>
      isNull ||
      _isStringObjectEmpty ||
      _isIterableObjectEmpty ||
      _isMapObjectEmpty ||
      _isBoolObjectFalse;

  /// Returns true if object is:
  /// - null `Object`
  /// - empty `String`
  /// - empty `Iterable` (list, map, set, ...)
  /// - false `bool`
  /// - zero `num`
  bool get isNullEmptyFalseOrZero =>
      isNull ||
      _isStringObjectEmpty ||
      _isIterableObjectEmpty ||
      _isMapObjectEmpty ||
      _isBoolObjectFalse ||
      _isNumObjectZero;

  // ------- PRIVATE EXTENSION HELPERS -------
  /// **Private helper**
  ///
  /// If `String` object, return String's method `isEmpty`
  ///
  /// Otherwise return `false` to not affect logical-OR expression. As `false` denotes undefined or N/A since object is not `String`
  bool get _isStringObjectEmpty =>
      (this is String) ? (this as String).isEmpty : false;

  /// **Private helper**
  ///
  /// If `Iterable` object, return Iterable's method `isEmpty`
  ///
  /// Otherwise return `false` to not affect logical-OR expression. As `false` denotes undefined or N/A since object is not `Iterable`
  bool get _isIterableObjectEmpty =>
      (this is Iterable) ? (this as Iterable).isEmpty : false;

  /// **Private helper**
  ///
  /// If `Map` object, return Map's method `isEmpty`
  ///
  /// Otherwise return `false` to not affect logical-OR expression. As `false` denotes undefined or N/A since object is not `Map`
  bool get _isMapObjectEmpty => (this is Map) ? (this as Map).isEmpty : false;

  /// **Private helper**
  ///
  /// If `bool` object, return `isFalse` expression
  ///
  /// Otherwise return `false` to not affect logical-OR expression. As `false` denotes undefined or N/A since object is not `bool`
  bool get _isBoolObjectFalse =>
      (this is bool) ? (this as bool) == false : false;

  /// **Private helper**
  ///
  /// If `num` object, return `isZero` expression
  ///
  /// Otherwise return `false` to not affect logical-OR expression. As `false` denotes undefined or N/A since object is not `num`
  bool get _isNumObjectZero => (this is num) ? (this as num) == 0 : false;
}

Assumptions

This presumes Dart 2.7 or above to support Extension Methods.

Usage

The above are two Extension classes. One for Object and one for String. Put them in a file separately/together and import the files/file at callsite file.

Description

Coming from Swift, I tend to use extensions like user @Benjamin Menrad's answer but with getter instead of method/function when applicable -- mirroring Dart's computed properties like String.isEmpty.

Coming from C#, I like their String's helper method IsNullOrWhiteSpace.

My version merges the above two concepts.

Naming

Rename the Extension classes whatever you like. I tend to name them like this:

XYExtension

Where:

  • X is File/Author/App/Unique name.
  • Y is name of Type being extended.

Name examples:

  • MyAppIntExtension
  • DartDoubleExtension
  • TextUtilsStringExtension
  • OHProviderExtension

Criticism

Why private getters instead of simple this == null || this == '' || this == [] || this == 0 || !this

  • I think this is safer as it first casts the object to the right type we want to compare its value to.
  • More modular as changes are central within the private getters and any modification is reflected everywhere.
  • If type check fails within the private getter, we return false which doesn't affect the outcome of the root logical-OR expression.
om-ha
  • 3,102
  • 24
  • 37
  • 1
    Fixed checking collections if they were empty or not with private getter `_isObjectIterableEmpty` – om-ha Feb 02 '20 at 23:14
  • 1
    Fixed another issue where `map` is not handled. I presumed it was a `Literal`, apparently it's not! Fixed is introduced with `_isMapObjectEmpty` private getter. – om-ha May 12 '20 at 16:28
  • 1
    You will not want to use `.isNull` or `isNotNull` in the future. With null safety, you can promote a nullable variable no non-nullable with a `== null` check, but not with `.isNull`. That makes `isNull` and `isNotNull` a dangerous habit to get into. – lrn Jun 11 '20 at 11:52
  • This is bad. I updated my answer accordingly. Your comment was confusing back in June, when I revisited today I got what you mean and explained it for others.. – om-ha Oct 08 '20 at 15:49
  • 1
    With null-safety, your extension should be on `String?` instead of `String` – Arash Aug 23 '21 at 14:07
  • Thanks @Arash, I will update my answer for the newest flutter and dart versions and inform you here in comments when I'm done. – om-ha Sep 21 '21 at 07:40
30

I would write a helper function instead of doing everything inline.

bool isNullEmptyOrFalse(Object o) =>
  o == null || false == o || "" == o;

bool isNullEmptyFalseOrZero(Object o) =>
  o == null || false == o || 0 == o || "" == o;

That avoids the repeated lookup (like the contains operation), but it is much more readable. It also doesn't create a new List literal for each check (making the list const could fix that).

if (isNullEmptyOrFalse(routeinfo["no_route"])) { ... }

When struggling with making something short and readable, making a well-named helper function is usually the best solution.

(Addition: Now that Dart has extension methods, it's possible to add the functionality as methods or getters that are seemingly on the object, so you can write value.isNullOrEmpty directly).

lrn
  • 64,680
  • 7
  • 105
  • 121
  • Modified the answer to add support for whitespace. Also check my full modified answer [below](https://stackoverflow.com/a/59826129/10830091). – om-ha Jan 20 '20 at 15:26
  • the lambda function worked great. bool isNullEmptyOrZero(Object o) => o == null || false == o || "" == o || 0==o; I added a check for 0 to exclude – Golden Lion Jun 10 '20 at 17:46
15

As coming from Android and Kotlin, I prefer extension methods over a static helper method. And with Dart 2.7 you can now use that as well:

extension Extension on Object {
  bool isNullOrEmpty() => this == null || this == '';

  bool isNullEmptyOrFalse() => this == null || this == '' || !this;

  bool isNullEmptyZeroOrFalse() =>
      this == null || this == '' || !this || this == 0;
}

Now you can just call these methods from anywhere in your code:

if (anyVariable.isNullOrEmpty()) {
  // do something here
}

You might need to manually import the dart class, where you put your extension methods, for example:

import 'package:sampleproject/utils/extensions.dart';
Benjamin Menrad
  • 898
  • 10
  • 14
  • the import get removed by android studio, probably it cannot detect the usage of it. any idea to work around it? – ITW Nov 12 '20 at 07:18
  • 2
    With "null safety" enabled (in Dart 2.12 beta), using one of the extension method proposed (e.g. `isNullOrEmpty`), on a nullable object leads to the error: `An expression whose value can be 'null' must be null-checked before it can be dereferenced.`. – DenisGL Nov 29 '20 at 17:52
  • if(testvariable == null){} condition does not work. Getting VSCode suggestion - The operand can't be null, so the condition is always false. Try removing the condition, an enclosing condition, or the whole conditional statement – Kamlesh May 15 '21 at 13:47
5

With Null safety:

Say, you have a nullable Map and a List which have nullable values in it.

Map<String, List?>? map;
List<String?>? list;

To check if the collection is neither null nor empty, you can do:

if (map?.containsKey('foo') ?? false) {
  print('map is not-null, and key "foo" is present.');
}

if (list?.isNotEmpty ?? false) {
  print('list is not-null and not empty');
} 
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
  • It looks terrible. Is it possible to write some extension func that works well with Null safety? – anber Jul 13 '22 at 09:19
4

For strings i like this approach:

extension NullableStringExtensions<E> on String? {
  /// Returns `true` if this string is `null` or empty.
  bool get isNullOrEmpty {
    return this?.isEmpty ?? true;
  }

  /// Returns `true` if this string is not `null` and not empty.
  bool get isNotNullNorEmpty {
    return this?.isNotEmpty ?? false;
  }
}

Credits go to the author of this package: https://pub.dev/packages/string_ext

valley
  • 157
  • 1
  • 13
2
bool isNullString(String? value) {
      if (value == null || value.isEmpty ) {
        return true;
      } else {
        return false;
      }
    }

and use this method like

isNullString(yourValue)
adarsh
  • 403
  • 3
  • 8
  • OK other than the `value == false` check; a condition which is not possible since `value` was declared as `String?`. Additionally, you can shorten this by getting rid of the `if/else` and simply returning `return value == null || value.isEmpty` – ubiquibacon Nov 29 '22 at 22:03
1

package:quiver has an isEmpty function that returns true if the argument is null or the empty string.

It's also trivial to implement such a function yourself.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
1

You can do an extension like this so you can use it everywhere:

extension StringExtensions on String? {
  bool isNullOrEmpty() {
    return this == null || this == "";
  }

}