is_dart_empty_or_not 0.2.3

SDKdartflutter
Platformandroidioswindowslinuxmacosweb

A Dart package that provides extensions for checking empty or zero values

is_dart_empty_or_not

License: MIT Coverage Status Pub Version

A minimal Dart package providing concise extension methods for handling empty and zero values on core types (String, List, Set, Map, num, int, double, Duration).

  • Purpose: Simplifies fallback logic and conditional actions for empty/zero values.
  • Origin: Inspired by Dart empty patterns [String.isEmpty, List.isEmpty etc..], see more about it Dart non-nullable initialization pattern

Features

  • Extension methods for String, List, Set, Map, num, int, double, Duration
  • Fallback and conditional logic in a single call
  • Zero dependencies

Quick Start

# pubspec.yaml
dependencies:
  is_dart_empty_or_not: ^0.2.3
import 'package:is_dart_empty_or_not/is_dart_empty_or_not.dart';

final name = ''.whenEmptyUse('Anonymous'); // 'Anonymous'
final items = <int>[].whenEmptyUse([1, 2]); // [1, 2]
final value = 0.whenZeroUse(42); // 42

''.onNotEmpty((s) => print('Not empty!')); // (does nothing)

API Overview

TypeMethodSignature / Description
StringwhenEmptyUseString whenEmptyUse(String value)
Returns the provided value if empty
StringonNotEmptyvoid onNotEmpty(void Function(String))
Runs callback if not empty
StringonEmptyvoid onEmpty(void Function(String))
Runs callback if empty
ListwhenEmptyUseList<T> whenEmptyUse(List<T> values)
Returns the provided value if empty
SetwhenEmptyUseSet<T> whenEmptyUse(Set<T> values)
Returns the provided value if empty
MapwhenEmptyUseMap<K,V> whenEmptyUse(Map<K,V> map)
Returns the provided value if empty
num/int/doublewhenZeroUsenum whenZeroUse(num value)
Returns fallback if zero
numisZerobool get isZero
True if value is zero
numisPositivebool get isPositive
True if value is positive
DurationisZerobool get isZero
True if duration is zero
DurationwhenZeroUseDuration whenZeroUse(Duration duration)
Returns fallback if zero
DurationisNegativebool get isNegative
True if duration is negative
DurationwhenNegativeUseDuration whenNegativeUse(Duration duration)
Returns fallback if negative
DurationisPositivebool get isPositive
True if duration is positive

Examples

String

''.whenEmptyUse('default'); // 'default'
'foo'.whenEmptyUse('default'); // 'foo'

'bar'.onNotEmpty((s) => print(s)); // prints 'bar'
''.onEmpty(() => print('foo')); // prints 'foo'

List

[].whenEmptyUse([1, 2]); // [1, 2]
[3].whenEmptyUse([1, 2]); // [3]

Set

<Set<int>>{}.whenEmptyUse({1, 2}); // {1, 2}
{3}.whenEmptyUse({1, 2}); // {3}

Map

<Map<int, String>>{}.whenEmptyUse({1: 'a'}); // {1: 'a'}
{2: 'b'}.whenEmptyUse({1: 'a'}); // {2: 'b'}

num/int/double

0.whenZeroUse(42); // 42
1.whenZeroUse(42); // 1

0.0.whenZeroUse(3.14); // 3.14
2.7.whenZeroUse(3.14); // 2.7

(0).isZero; // true
(1).isZero; // false

(5).isPositive; // true
(-2).isPositive; // false

Duration

Duration.zero.isZero; // true
Duration(seconds: 1).isZero; // false

Duration.zero.whenZeroUse(Duration(seconds: 5)); // 0:00:05.000000
Duration(seconds: 2).whenZeroUse(Duration(seconds: 5)); // 0:00:02.000000

Duration(seconds: -1).isNegative; // true
Duration(seconds: 1).isNegative; // false

Duration(seconds: -1).whenNegativeUse(Duration(seconds: 3)); // 0:00:03.000000
Duration(seconds: 2).whenNegativeUse(Duration(seconds: 3)); // 0:00:02.000000

Duration(seconds: 2).isPositive; // true
Duration(seconds: -1).isPositive; // false

Running the Example

This package includes an example project in the example directory. To run the example:

  1. Navigate to the root directory of the package.

  2. Execute the following command:

    dart run example/main.dart
    

This will run the main.dart file located in the example directory and print the output of the various extension method demonstrations to the console. Refer to example/README.md for more details.

Changelog

See CHANGELOG.md

License

This package is licensed under the MIT License - see the LICENSE file for details.