wyatt_type_utils (0.0.5)
Installation
dart pub add wyatt_type_utils:0.0.5 --hosted-url=
About this package
Wyatt Type Utils
Either, Option and other useful types and extensions for Dart (and Flutter).
Option<T>
Option is a container object which may or may not contain a non-null value. If a Value is present, isPresent()
will return true and get()
will return the value. Additional methods that depend on the presence or absence of a contained value are provided, such as orElse()
(return a default value if value not present) and ifPresent()
(execute a block of code if the value is present).
final Option<int> rand = Option.ofNullable(
Random().nextBool() ? null : 10,
);
print(
"`rand` is${rand.isNull ? "" : " not"} null, "
"so it's value is ${rand.orElse(15)}",
);
// Ok, `orElse` is equivalent to ?? but the Option is
// useful for match pattern and other utils accesses.
rand.match((value) {
print('Value is: $value');
}, () {
print('Rand is null');
},
);
print('`rand` contains 10 ? => ${rand.contains(10)}');
Result<T, E>
Result type is coming from functional languages where exceptions are (rightfully) considered a side-effect, and therefore not appropriate to pass domain errors. Mind the difference between different kinds of errors: Some of them belong to domain, others don't.
E.g. null reference exception or index out of bounds are not related to domain - they rather indicate a defect.
Either is defined as a generic type with two branches
- success with T
- failure with E
It can appear in two forms, where it contains an object of Ok, or where it contains an object of Err. It cannot appear in both states at once, or in none of them. Therefore, if one possesses an Result instance, it either contains a successfully produced result, or contains an error object.
Future<Result<TodoModel, AppError>> requestTodo() async => Result.tryCatchAsync(
() => get('https://jsonplaceholder.typicode.com/todos/1'),
(error) => ServerError('Error while getting todo'),
);
final Result<TodoModel, AppError> todo1 = await requestTodo();
// In Flutter:
todo1.either(buildWidgetWithTodo, buildSizedBox);
Pair<L, R>
Pair is a simple object which contains pair of two values.
const Pair<int, int> myPair = Pair(42, 16);
print(myPair.left); // prints '42'
print(myPair.right); // prints '16'
print(myPair); // prints '(42, 16)'
final List<int> myList = myPair.toList();
print(myList); // prints '[42, 16]'
Extensions
This package also provides extensions for Dart types.
- Object
- isNull
- isNotNull
- log
- Iterable
- isNullOrEmpty
- isNotNullOrEmpty
- For bytes iterables
- toTypedList
- toStr
- String
- isNullOrEmpty
- isNotNullOrEmpty
- toBytes
String 🔁 Uint8List works with encoding:
const String myString = 'abc';
final Uint8List bytes = myString.toBytes(from : Encoding.utf16);
print(bytes.toStr(to : Encoding.utf16)); // prints 'abc'
- DateTime
- fromSecondsSinceEpoch
- secondsSinceEpoch
- timestamp
- tomorrow
- yesterday
- today
- add/sub/set
- nextDay
- previousDay
- nextMonth
- previousMonth
- nextYear
- previousYear
- nextWeek
- previousWeek
- isFuture
- isPast
- isLeapYear
- format
- operators
The date formatter works with String
formatter:
final DateTime date = DateTime(1999, 6, 30);
print(date.format([dd, '/', mm, '/', yy])); // prints '30/06/99'
Allow comparison between nullable numbers:
final int? a = 10;
final int? b = 20;
print(a < b); // prints 'true'