Nullx
🔧 nullx: A Dart toolkit designed to enhance handling of nullable types, providing utilities for null-checking, navigating nullable structures, and ensuring safer, more robust code.
Install / Use
/learn @ashtanko/NullxREADME
nullx is a dart toolkit that enhances handling of nullable types, providing utilities for null-checking, navigating nullable structures, and robust error handling, for cleaner and more resilient code.
Features
- Provides utilities for null-checking
- Helps in navigating nullable structures
- Offers robust error handling
- Nullable types extensions
Getting started 🎉
To use this package, add nullx as a dependency in your pubspec.yaml file.
dependencies:
nullx: ^latest_version
Here is a simple example of how to use nullx:
Collections
import 'package:nullx/nullx.dart';
void main() {
final list = [1, null, 3, null];
list.mapNonNull((item) => item * 2); // prints: [2, 6]
}
import 'package:nullx/nullx.dart';
void main() {
final list = [1, null, 3, null];
list.mapNonNullIndexed((item, index) => item * index); // prints: [0, 6]
}
import 'package:nullx/nullx.dart';
void main() {
final List<int>? nullableList = [1, 2, 3];
nullableList.whatIfNotNullOrEmpty(
(list) => print(list), // whatIf
() => print('List is null or empty'), // whatIfNot
);
// prints: [1, 2, 3]
}
import 'package:nullx/nullx.dart';
void main() {
List<int>? nullableList = [1, 2, 3];
print(nullableList.isNullOrEmpty); // prints: false
nullableList = null;
print(nullableList.isNullOrEmpty); // prints: true
nullableList = [];
print(nullableList.isNullOrEmpty); // prints: true
}
Types
import 'package:nullx/nullx.dart';
void main() {
const String? nullableString = 'example';
unwrapped(nullableString, (value) {});
final List<String?> strings = [null, 'one', null, 'two', null];
strings.map((s) => s.letNonNull((s) => s.length)).whereType<int>().toList();
}
import 'package:nullx/nullx.dart';
void main() {
const int? nullableInt = 10;
final resultInt = nullableInt.letNonNull((value) => value * 2); // prints: 2
}
import 'package:nullx/nullx.dart';
void main() {
const int userAge = 20;
executeIf(
() => userAge >= 18,
onConditionMet: () {
// prints 'You are an adult.'
},
onConditionNotMet: () {
// prints 'You are not an adult.'
},
);
}
import 'package:nullx/nullx.dart';
void main() {
var userAge = 20;
final result = executeIfAs<String>(
() => userAge >= 18,
onConditionMet: () => 'You are an adult.',
onConditionNotMet: () => 'You are not an adult.',
);
}
import 'package:nullx/nullx.dart';
void main() {
void printValue(String value) {
// prints 'The value is: $value'
}
notEmpty(nullableString, printValue);
}
import 'package:nullx/nullx.dart';
void main() {
const String? nullableString = 'example';
unwrapped(nullableString, (value) {}); // prints: 'example'
}
import 'package:nullx/nullx.dart';
void main() {
var nullableString = 'Hello';
nullableString.notEmpty((item) => print(item)); // prints: Hello
}
import 'package:nullx/nullx.dart';
void main() {
const int userAge = 20;
// Unwraps the nullable string and performs an operation on it
callWhen(
condition: () => userAge >= 18,
onMet: () {
// prints 'You are an adult.'
},
onNotMet: () {
// prints 'You are not an adult.'
},
);
}
import 'package:nullx/nullx.dart';
void main() {
const int? nullableInt = null;
nullableInt.orZero; // Outputs: 0
nullableInt.or(defaultValue: 5); // Outputs: 5
const double? nullableDouble = null;
nullableDouble.orZero; // Outputs: 0.0
nullableDouble.or(defaultValue: 5.5); // Outputs: 5.5
const bool? nullableBool = null;
nullableBool.orFalse; // Outputs: false
nullableBool.or(defaultValue: true); // Outputs: true
}
void main() {
final Map<String, int>? nullableMap = {'a': 1, 'b': 2};
print(nullableMap.isNullOrEmpty); // false
print(nullableMap.getOrElse('c', 0)); // 0
nullableMap.putIfAbsentOrElse('c', 3); // {a: 1, b: 2, c: 3}
nullableMap.updateValue('a', (value) => value! + 10); // {a: 11, b: 2, c: 3}
final filteredMap = nullableMap.filter((entry) => entry.value > 2);
print(filteredMap); // {a: 11, c: 3}
}
Contributing
Contributions are welcome! Please read the contributing guide to learn how to contribute to the project and set up a development environment.
License
Copyright 2024 Oleksii Shtanko
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Related Skills
diffs
344.1kUse the diffs tool to produce real, shareable diffs (viewer URL, file artifact, or both) instead of manual edit summaries.
clearshot
Structured screenshot analysis for UI implementation and critique. Analyzes every UI screenshot with a 5×5 spatial grid, full element inventory, and design system extraction — facts and taste together, every time. Escalates to full implementation blueprint when building. Trigger on any digital interface image file (png, jpg, gif, webp — websites, apps, dashboards, mockups, wireframes) or commands like 'analyse this screenshot,' 'rebuild this,' 'match this design,' 'clone this.' Skip for non-UI images (photos, memes, charts) unless the user explicitly wants to build a UI from them. Does NOT trigger on HTML source code, CSS, SVGs, or any code pasted as text.
openpencil
2.0kThe world's first open-source AI-native vector design tool and the first to feature concurrent Agent Teams. Design-as-Code. Turn prompts into UI directly on the live canvas. A modern alternative to Pencil.
HappyColorBlend
HappyColorBlendVibe Project Guidelines Project Overview HappyColorBlendVibe is a Figma plugin for color palette generation with advanced tint/shade blending capabilities. It allows designers to
