File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
tipsandtricks/unwrapping-multiple-optionals-in-flutter-and-dart Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
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 >());
You can’t perform that action at this time.
0 commit comments