Created
May 10, 2020 17:00
-
-
Save ericwindmill/e9bfbab75fc99e90bb36b81d94a775cc to your computer and use it in GitHub Desktop.
fbe_dart_iterables_change_expand
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
Try using `expand` to transform a list of orders into a list of those order's items. |
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
/// 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 | |
} |
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
List<Item> extractItemsFromOrdersForCustomer(Customer customer) { | |
final items = customer.pastOrders.expand((Order o) => o.itemsPurchased).toList(); | |
return items; | |
} |
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
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