Skip to content

Add isNullOrEmpty Getter for Nullable String Instances #60000

@stan-at-work

Description

@stan-at-work

Currently, there is no direct way to check if a nullable string is either null or empty ('') without writing verbose code. Adding an isNullOrEmpty getter for nullable string instances could simplify this common check and enhance code readability.

Example Usage:

String? str;

// Instead of writing:
if (str == null || str.isEmpty) {
  // Do something
}

// With the proposed getter:
if (str.isNullOrEmpty) {
  // Do something
}

Benefits:

  1. Improved readability: Reduces boilerplate code and improves clarity.
  2. Consistency: Aligns with other commonly used utility methods in the Dart ecosystem.
  3. Ease of use: Provides a direct and intuitive way to handle nullable strings.

Implementation Consideration:
This getter could be implemented as an extension on String? to ensure minimal impact on existing codebases.

Example Implementation:

extension NullableStringExtensions on String? {
  bool get isNullOrEmpty => this == null || this.isEmpty;
}

This is how allot of peaple do this at the moment, this could be part of the sdk for conveniance.

I believe this feature would be a valuable addition to the Dart language.

I know this is a duplicate of (#11155) but that issue had aged over 13 years...

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.triage-automationSee https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot.type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions