Created
July 21, 2023 09:30
-
-
Save VB10/86ea71db61b96b9b49b7af4bd9e8452b to your computer and use it in GitHub Desktop.
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
extension IterableExtension<T> on Iterable<T?> { | |
List<T> makeSafe() { | |
return where((element) => element != null).cast<T>().toList(); | |
} | |
} | |
class VB10ViewModel { | |
List<User> dummyListToSafe() { | |
final userList = <User?>[ | |
const User(), | |
null, | |
const User(), | |
null, | |
const User() | |
]; | |
final safeUserList = userList.makeSafe(); | |
return safeUserList; | |
} | |
} | |
class VB10View extends StatelessWidget { | |
const VB10View({super.key}); | |
static final viewModel = VB10ViewModel(); | |
@override | |
Widget build(BuildContext context) { | |
return ListView.builder( | |
itemCount: viewModel.dummyListToSafe().length, | |
itemBuilder: (BuildContext context, int index) { | |
return UserCard(user:viewModel.dummyListToSafe()[index]); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment