is_dart_empty_or_not
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
| Type | Method | Signature / Description |
|---|---|---|
| String | whenEmptyUse | String whenEmptyUse(String value)Returns the provided value if empty |
| String | onNotEmpty | void onNotEmpty(void Function(String))Runs callback if not empty |
| String | onEmpty | void onEmpty(void Function(String))Runs callback if empty |
| List | whenEmptyUse | List<T> whenEmptyUse(List<T> values)Returns the provided value if empty |
| Set | whenEmptyUse | Set<T> whenEmptyUse(Set<T> values)Returns the provided value if empty |
| Map | whenEmptyUse | Map<K,V> whenEmptyUse(Map<K,V> map)Returns the provided value if empty |
| num/int/double | whenZeroUse | num whenZeroUse(num value)Returns fallback if zero |
| num | isZero | bool get isZeroTrue if value is zero |
| num | isPositive | bool get isPositiveTrue if value is positive |
| Duration | isZero | bool get isZeroTrue if duration is zero |
| Duration | whenZeroUse | Duration whenZeroUse(Duration duration)Returns fallback if zero |
| Duration | isNegative | bool get isNegativeTrue if duration is negative |
| Duration | whenNegativeUse | Duration whenNegativeUse(Duration duration)Returns fallback if negative |
| Duration | isPositive | bool get isPositiveTrue 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:
-
Navigate to the root directory of the package.
-
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.