Skip to content

Commit

Permalink
Create optional-iterable-first-element-in-dart.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
vandadnp committed Nov 16, 2022
1 parent d66b899 commit 076b7b9
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 🐦 Twitter https://twitter.com/vandadnp
// πŸ”΅ LinkedIn https://linkedin.com/in/vandadnp
// πŸŽ₯ YouTube https://youtube.com/c/vandadnp
// πŸ’™ Free Flutter Course https://linktr.ee/vandadnp
// πŸ“¦ 11+ Hours Bloc Course https://youtu.be/Mn254cnduOY
// πŸ”Ά 7+ Hours MobX Course https://youtu.be/7Od55PBxYkI
// πŸ¦„ 8+ Hours RxSwift Coursde https://youtu.be/xBFWMYmm9ro
// 🀝 Want to support my work? https://buymeacoffee.com/vandad

import 'dart:developer' as devtools show log;

extension Log on Object? {
void log() =>
devtools.log(this?.toString() ?? '\x1b[101m\x1b[30mNULL\x1b[0m');
}

extension OptionalFirst<T> on Iterable<T> {
T? get optionalFirst => isEmpty ? null : first;
}

void printName([
String? name,
List<String>? orFirstNameFoundInList,
]) {
name ??=
orFirstNameFoundInList?.optionalFirst
?? 'No name is found';
name.log();
}

void testIt() {
printName(); // prints 'No name is found'
printName('Foo'); // prints Foo
printName(null, []); // prints 'No name is found'
printName(null, ['Bar']); // prints Bar
}

0 comments on commit 076b7b9

Please sign in to comment.