diff --git a/tipsandtricks/unwrapping-multiple-optionals-in-flutter-and-dart/unwrapping-multiple-optionals-in-flutter-and-dart.dart b/tipsandtricks/unwrapping-multiple-optionals-in-flutter-and-dart/unwrapping-multiple-optionals-in-flutter-and-dart.dart new file mode 100644 index 0000000..93bff2c --- /dev/null +++ b/tipsandtricks/unwrapping-multiple-optionals-in-flutter-and-dart/unwrapping-multiple-optionals-in-flutter-and-dart.dart @@ -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 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( + List optionals, + T Function(List) callback, +) => + optionals.any((e) => e == null) ? null : callback(optionals.cast());