Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🔧 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.

License

NotificationsYou must be signed in to change notification settings

ashtanko/nullx

Repository files navigation


CoverageDart CIVery Good Workflowpub.devCodeFactorCodacy BadgeCodecovCodacy BadgeLicense: MIT


nullx is adart 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, addnullx 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';voidmain() {final list= [1,null,3,null];  list.mapNonNull((item)=> item*2);// prints: [2, 6]}
import'package:nullx/nullx.dart';voidmain() {final list= [1,null,3,null];  list.mapNonNullIndexed((item, index)=> item* index);// prints: [0, 6]}
import'package:nullx/nullx.dart';voidmain() {finalList<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';voidmain() {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';voidmain() {constString? nullableString='example';unwrapped(nullableString, (value) {});finalList<String?> strings= [null,'one',null,'two',null];  strings.map((s)=> s.letNonNull((s)=> s.length)).whereType<int>().toList();}
import'package:nullx/nullx.dart';voidmain() {constint? nullableInt=10;final resultInt= nullableInt.letNonNull((value)=> value*2);// prints: 2}
import'package:nullx/nullx.dart';voidmain() {constint userAge=20;executeIf(    ()=> userAge>=18,    onConditionMet: () {// prints 'You are an adult.'    },    onConditionNotMet: () {// prints 'You are not an adult.'    },  );}
import'package:nullx/nullx.dart';voidmain() {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';voidmain() {voidprintValue(String value) {// prints 'The value is: $value'    }notEmpty(nullableString, printValue);}
import'package:nullx/nullx.dart';voidmain() {constString? nullableString='example';unwrapped(nullableString, (value) {});// prints: 'example'}
import'package:nullx/nullx.dart';voidmain() {var nullableString='Hello';    nullableString.notEmpty((item)=>print(item));// prints: Hello}
import'package:nullx/nullx.dart';voidmain() {constint userAge=20;// Unwraps the nullable string and performs an operation on itcallWhen(    condition: ()=> userAge>=18,    onMet: () {// prints 'You are an adult.'    },    onNotMet: () {// prints 'You are not an adult.'    },  );}
import'package:nullx/nullx.dart';voidmain() {constint? nullableInt=null;    nullableInt.orZero;// Outputs: 0    nullableInt.or(defaultValue:5);// Outputs: 5constdouble? nullableDouble=null;    nullableDouble.orZero;// Outputs: 0.0    nullableDouble.or(defaultValue:5.5);// Outputs: 5.5constbool? nullableBool=null;    nullableBool.orFalse;// Outputs: false    nullableBool.or(defaultValue:true);// Outputs: true}
voidmain() {finalMap<String,int>? nullableMap= {'a':1,'b':2};print(nullableMap.isNullOrEmpty);// falseprint(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 ShtankoLicensed 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.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License.

About

🔧 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.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp