nullx_function 0.2.2
nullx_function: ^0.2.2 copied to clipboard
nullx_function is a collection of elegant extensions for handling null types on Dart function types.
nullx_function - dart nullability with confidence
nullx_function 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
- Nullable types extensions
Getting started 🎉 #
To use this package, add nullx_function
as a dependency in your pubspec.yaml file.
dependencies:
nullx_function: ^latest_version
Here is a simple example of how to use nullx_function:
import 'package:nullx_function/nullx_function.dart';
void main() async {
// Example 1: callIfNotNull
void greet(String name) {
// ignore: avoid_print
print('Hello, $name!');
}
Function? nullableFunction = greet;
// ignore: avoid_print
print('Example 1: callIfNotNull');
nullableFunction.callIfNotNull(['Alice']); // Output: Hello, Alice!
nullableFunction = null;
nullableFunction.callIfNotNull(['Bob']); // No output
// ignore: avoid_print
print('\nExample 2: callWithOneArg');
// Example 2: callWithOneArg
void printMessage(String message) {
// ignore: avoid_print
print('Message: $message');
}
nullableFunction = printMessage;
nullableFunction
.callWithOneArg('This is a test.'); // Output: Message: This is a test.
nullableFunction = null;
nullableFunction.callWithOneArg('This won’t be printed.'); // No output
// ignore: avoid_print
print('\nExample 3: callOrDefault');
// Example 3: callOrDefault
int doubleValue(int value) => value * 2;
nullableFunction = doubleValue;
int? result = nullableFunction.callOrDefault<int>([5], -1); // Returns: 10
// ignore: avoid_print
print('Double value of 5: $result');
nullableFunction = null;
result = nullableFunction.callOrDefault<int>([5], -1); // Returns: -1
// ignore: avoid_print
print('Default value: $result');
// ignore: avoid_print
print('\nExample 4: callAndReturn');
// Example 4: callAndReturn
String concatenate({required String a, required String b}) => '$a$b';
nullableFunction = concatenate;
String? concatenated = nullableFunction.callAndReturn<String>([], {
#a: 'Hello, ',
#b: 'World!',
}); // Returns: Hello, World!
// ignore: avoid_print
print('Concatenated result: $concatenated');
nullableFunction = null;
concatenated = nullableFunction.callAndReturn<String>([], {
#a: 'This ',
#b: 'won’t be concatenated.',
}); // Returns: null
// ignore: avoid_print
print('Concatenated result when function is null: $concatenated');
}
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.