check-if-an-object-is-truthy-in-dart

Check if an Object is Truthy in Dart

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "check-if-an-object-is-truthy-in-dart" with this command: npx skills add rodydavis/skills/rodydavis-skills-check-if-an-object-is-truthy-in-dart

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.

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

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

flutter-control-and-screenshot

No summary provided by upstream source.

Repository SourceNeeds Review
General

install-flutter-from-git

No summary provided by upstream source.

Repository SourceNeeds Review
General

how-to-build-a-native-cross-platform-project-with-flutter

No summary provided by upstream source.

Repository SourceNeeds Review
General

how-to-build-a-webrtc-signal-server-with-pocketbase

No summary provided by upstream source.

Repository SourceNeeds Review