Skip to content

Instantly share code, notes, and snippets.

@ericwindmill
Created May 10, 2020 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericwindmill/e9bfbab75fc99e90bb36b81d94a775cc to your computer and use it in GitHub Desktop.
Save ericwindmill/e9bfbab75fc99e90bb36b81d94a775cc to your computer and use it in GitHub Desktop.
fbe_dart_iterables_change_expand
Try using `expand` to transform a list of orders into a list of those order's items.
/// These classes do not need to be changed.
class Customer {
final List<Order> pastOrders;
Customer(this.pastOrders);
}
class Order {
final List<Item> itemsPurchased;
Order(this.itemsPurchased);
}
class Item {
final String name;
final int cost;
Item(this.name, this.cost);
}
/// Start your edits here
List<Item> extractItemsFromOrdersForCustomer(Customer customer) {
// implement
}
List<Item> extractItemsFromOrdersForCustomer(Customer customer) {
final items = customer.pastOrders.expand((Order o) => o.itemsPurchased).toList();
return items;
}
void main() {
final allLineItemsFromCustomer = extractItemsFromOrdersForCustomer(customer);
if (allLineItemsFromCustomer is! List<Item>) {
_result(false, ['the function did not return a List<Item> type']);
}
if (allLineItemsFromCustomer.length != 4) {
_result(false, [
'the function extractItemsFromOrdersForCustomer should have returned a list of length 4',
'Got a list of length ${allLineItemsFromCustomer.length}.'
]);
}
_result(true, ['All tests passed!', 'Good job, fam']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment