home / skills / rodydavis / skills / dart_truthy

dart_truthy skill

/skills/dart_truthy

This skill helps you implement truthy checks in Dart by extending Object to evaluate null, booleans, numbers, strings, and collections.

npx playbooks add skill rodydavis/skills --skill dart_truthy

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
1.6 KB
---
name: check-if-an-object-is-truthy-in-dart
description: Learn how to extend Dart's functionality to implement JavaScript-style "truthy" checks for easier conditional logic and value evaluations.
metadata:
  url: https://rodydavis.com/posts/dart/truthy
  last_modified: Tue, 03 Feb 2026 20:04:34 GMT
---

# Check if an Object is Truthy in Dart


If you are coming from language like JavaScript you may be used to checking if an object is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy).

```
if (true)
if ({})
if ([])
if (42)
if ("0")
if ("false")
if (new Date())
if (-42)
if (12n)
if (3.14)
if (-3.14)
if (Infinity)
if (-Infinity)
```

In Dart you need to explicitly check if an object is not null, true/false or determine if the value is true based on the type.

It is possible however to use Dart extensions to add the truthy capability.

```
extension on Object? {
  bool get isTruthy => truthy(this);
}

bool truthy(Object? val) {
  if (val == null) return false;
  if (val is bool) return val;
  if (val is num && val == 0) return false;
  if (val is String && (val == 'false' || val == '')) return false;
  if (val is Iterable && val.isEmpty) return false;
  if (val is Map && val.isEmpty) return false;
  return true;
}
```

This will now make it possible for any object to be evaluated as a truthy value in if statements or value assignments.

Prints the following:

```
(null, false)
(, false)
(false, false)
(true, true)
(0, false)
(1, true)
(false, false)
(true, true)
([], false)
([1, 2, 3], true)
({}, false)
({1, 2, 3}, true)
({a: 1, b: 2}, true)
```

## Demo

Overview

This skill shows how to extend Dart with a JavaScript-style "truthy" check so values can be evaluated more ergonomically in conditionals. It provides a small extension and a truthy helper function to interpret nulls, booleans, numbers, strings, iterables, and maps. Use it to simplify conditional logic and normalize value checks across different types.

How this skill works

The core is a Dart extension on Object? that exposes an isTruthy getter which delegates to a truthy(Object?) function. The truthy function returns false for null, false boolean, numeric zero, empty strings or the string "false", empty iterables, and empty maps; everything else is treated as true. Once added, you can call value.isTruthy in if statements or assignments to get consistent truthiness semantics.

When to use it

  • When porting JavaScript logic that relies on truthy/falsy evaluation to Dart.
  • When you want concise conditionals without repeated null/empty checks.
  • When normalizing input from dynamic sources (JSON, user input) before branching logic.
  • When writing utility-heavy code where short, readable checks improve maintainability.

Best practices

  • Keep the truthy rules explicit and documented for your team so behavior is predictable.
  • Avoid using truthy for business-critical validations—use explicit checks for domain rules.
  • Limit extension scope to Object? and a clearly named getter like isTruthy to reduce surprise.
  • Write unit tests covering edge cases (zero, empty collections, strings like "false").
  • Consider making the function configurable if you need different falsy semantics in different modules.

Example use cases

  • Simplify UI branching: if (data.isTruthy) show content else show placeholder.
  • Validate form fields where empty strings and zero are considered empty: if (!value.isTruthy) show validation error.
  • Process JSON payloads: treat missing, empty arrays, and "false" as falsy in downstream logic.
  • Migrate JS helpers: replace repetitive null/empty checks with a single isTruthy call for readability.

FAQ

Does this change Dart's language semantics?

No. It only adds a convenience extension and function; it does not alter Dart's built-in truthiness rules or control flow semantics.

Will isTruthy treat "false" string as false?

Yes. The implementation treats the literal string "false" and the empty string as falsy to mirror common JS-like patterns.