-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create unwrapping-multiple-optionals-in-flutter-and-dart.dart
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...iple-optionals-in-flutter-and-dart/unwrapping-multiple-optionals-in-flutter-and-dart.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>()); |