Skip to content

Commit 4194295

Browse files
authored
Create unwrapping-multiple-optionals-in-flutter-and-dart.dart
1 parent a85b4c2 commit 4194295

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// 🐦 Twitter https://twitter.com/vandadnp
2+
// 🔵 LinkedIn https://linkedin.com/in/vandadnp
3+
// 🎥 YouTube https://youtube.com/c/vandadnp
4+
// 💙 Free Flutter Course https://linktr.ee/vandadnp
5+
// 📦 11+ Hours Bloc Course https://youtu.be/Mn254cnduOY
6+
// 🔶 7+ Hours MobX Course https://youtu.be/7Od55PBxYkI
7+
// 🦄 8+ Hours RxSwift Coursde https://youtu.be/xBFWMYmm9ro
8+
// 🤝 Want to support my work? https://buymeacoffee.com/vandad
9+
10+
void main(List<String> args) {
11+
/// unwrapping multiple optionals
12+
print(getFullName(null, null)); // Empty
13+
print(getFullName('John', null)); // Empty
14+
print(getFullName(null, 'Doe')); // Empty
15+
print(getFullName('John', 'Doe')); // John Doe
16+
}
17+
18+
String getFullName(
19+
String? firstName,
20+
String? lastName,
21+
) =>
22+
withAll([
23+
firstName,
24+
lastName,
25+
], (names) => names.join(' ')) ??
26+
'Empty';
27+
28+
T? withAll<T>(
29+
List<T?> optionals,
30+
T Function(List<T>) callback,
31+
) =>
32+
optionals.any((e) => e == null) ? null : callback(optionals.cast<T>());

0 commit comments

Comments
 (0)