opensourceprojects.dev

A broadsheet for software that doesn't ask for your email

nullx: cleaner null-checking and nullable navigation for Dart
GitHub RepoImpressions2

Project Description

View on GitHub

Stop Wrestling With Null Checks in Dart: Meet nullx

You know the drill. You're writing Dart code, and you've got a nullable value that needs to be transformed, filtered, or passed along—but first you have to unwrap it, check if it's null, maybe check if the collection is empty, and suddenly a one-liner becomes a five-line nested if-statement. It's not hard, exactly, but it's noisy. It clutters your logic and makes your code harder to read than it needs to be.

That's where nullx comes in. It's a Dart toolkit that gives you cleaner utilities for null-checking, navigating nullable structures, and handling errors—without changing how Dart's type system works. You still get all the safety of null-aware code; you just write less boilerplate to get there.

What It Does

nullx is a Dart package that extends your toolkit with nullable-type helpers. At its core, it provides extension methods and utility functions that make common null-handling patterns more concise and expressive.

The package focuses on a few key areas. For collections, it gives you ways to work with lists that might contain nulls or might themselves be null. For individual values, it offers extensions like letNonNull that let you apply a function only when a value isn't null. There's also executeIf for conditional logic, and unwrapped for safely accessing nullable values.

The API is designed to read naturally. Instead of writing a guard clause, you chain a method. Instead of checking if (list != null && list.isNotEmpty), you call whatIfNotNullOrEmpty and pass two callbacks: one for the happy path, one for the fallback. It's a small shift in syntax, but it changes how your code flows—less nesting, more linear logic.

Why It's Cool

The real appeal of nullx is how it composes. Once you start using these extensions, you'll notice your null-handling code starts to look like a pipeline rather than a series of checks.

Here's what stands out:

  • It attacks the noise, not the safety. Dart's null safety is a feature, not a bug. nullx doesn't try to bypass it—it just makes the ergonomics better. You're still writing type-safe code; you're just writing less of the ceremony around it.

  • The collection helpers are genuinely useful. Take mapNonNull. It filters out nulls and maps in one pass. No more where((item) => item != null).map((item) => item!) chains. There's also an indexed version, mapNonNullIndexed, which gives you the index without the null-checking headache.

  • isNullOrEmpty is the kind of helper you didn't know you needed. It works on nullable lists and returns true if the list is null OR empty. That's a deceptively simple check that appears everywhere in real apps, and now it's one property instead of a compound condition.

  • The whatIfNotNullOrEmpty pattern is elegant. Instead of if (list != null && list.isNotEmpty) { ... } else { ... }, you get a single method with two clear callbacks. The intent is explicit, and the branching is contained.

  • letNonNull brings a functional flavor to Dart. If you're coming from Kotlin or Swift, you'll recognize this pattern immediately. It's a clean way to transform a nullable value only when it exists, and it chains beautifully with other operations.

The package is also well-maintained—it has continuous integration, code coverage tracking, and a solid license. That matters when you're adding a dependency to your project.

How to Try It

Getting started is straightforward. Add nullx to your pubspec.yaml:

dependencies:
  nullx: ^latest_version

Then import it and start using the extensions. Here's a quick taste:

import 'package:nullx/nullx.dart';

void main() {
  final list = [1, null, 3, null];
  list.mapNonNull((item) => item * 2); // [2, 6]
}

Or with an indexed version:

final list = [1, null, 3, null];
list.mapNonNullIndexed((item, index) => item * index); // [0, 6]

For nullable collections:

List<int>? nullableList = [1, 2, 3];
print(nullableList.isNullOrEmpty); // false

nullableList = null;
print(nullableList.isNullOrEmpty); // true

And for individual values:

const int? nullableInt = 10;
final resultInt = nullableInt.letNonNull((value) => value * 2);

You can find the full documentation and more examples in the repository on GitHub.

Final Thoughts

nullx isn't going to rewrite your understanding of Dart, and it doesn't need to. What it does is take a common pain point—verbose null handling—and give you cleaner, more readable alternatives. If you're building Flutter apps or Dart services where null checks seem to multiply, this package is worth a look. It's small, focused, and it respects the language's type system while making your code more pleasant to write and read.

Give it a try on your next project. You might find that the null checks you used to dread become just another part of the flow.


Follow @githubprojects for more developer tools and open source projects.

Back to Projects
Project ID: c1d4125c-4ed3-4b1a-88ae-b98c3e4d252fLast updated: September 1, 2026 at 06:13 AM