Skip to content

Instantly share code, notes, and snippets.

@rozd
Last active May 19, 2021 09:26
Show Gist options
  • Save rozd/2bfda64c87e001a15f8cd6b76d9e9678 to your computer and use it in GitHub Desktop.
Save rozd/2bfda64c87e001a15f8cd6b76d9e9678 to your computer and use it in GitHub Desktop.
import 'package:flutter/widgets.dart';
// MARK: - Iterable Extensions
extension FlutterishIterable<T> on Iterable<T> {
List<Widget> toWidgets({
required Widget Function(T element) itemBuilder,
Widget Function()? separatorBuilder
}) {
if (separatorBuilder == null) {
return this.map((e) => itemBuilder(e)).toList();
}
return this.cast<T?>()
.expand((e) => e == this.last ? [e] : [e, null])
.map((e) => e != null ? itemBuilder(e) : separatorBuilder())
.cast<Widget>()
.toList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment