Skip to content

Commit

Permalink
Create unwrapping-multiple-optionals-in-flutter-and-dart.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
vandadnp authored Nov 24, 2022
1 parent a85b4c2 commit 4194295
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 🐦 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

void main(List<String> args) {
/// unwrapping multiple optionals
print(getFullName(null, null)); // Empty
print(getFullName('John', null)); // Empty
print(getFullName(null, 'Doe')); // Empty
print(getFullName('John', 'Doe')); // John Doe
}

String getFullName(
String? firstName,
String? lastName,
) =>
withAll([
firstName,
lastName,
], (names) => names.join(' ')) ??
'Empty';

T? withAll<T>(
List<T?> optionals,
T Function(List<T>) callback,
) =>
optionals.any((e) => e == null) ? null : callback(optionals.cast<T>());

0 comments on commit 4194295

Please sign in to comment.