Challenge: Find product
Part of the Fundamentals section of Coddy's Dart journey — lesson 94 of 94.
Challenge
EasyIn this challenge, you'll practice using the find method with lists to locate specific items based on conditions.
You have a list of products in a shopping cart, represented as maps with name, price, and quantity. Your task is to complete the findProduct function that searches for a product by name in the cart.
The function should return the first product that matches the given name, or null if no match is found.
Complete the code to properly implement this search functionality.
Try it yourself
void main() {
// Shopping cart with products
List<Map<String, dynamic>> shoppingCart = [
{'name': 'Laptop', 'price': 999.99, 'quantity': 1},
{'name': 'Headphones', 'price': 59.99, 'quantity': 2},
{'name': 'Mouse', 'price': 24.99, 'quantity': 1},
{'name': 'Keyboard', 'price': 49.99, 'quantity': 1}
];
// Test the findProduct function
Map<String, dynamic>? foundProduct = findProduct(shoppingCart, 'Headphones');
if (foundProduct != null) {
print('Found: ${foundProduct['name']} - \$${foundProduct['price']} (Quantity: ${foundProduct['quantity']})');
} else {
print('Product not found');
}
// Test with a product that doesn't exist
foundProduct = findProduct(shoppingCart, 'Monitor');
if (foundProduct != null) {
print('Found: ${foundProduct['name']} - \$${foundProduct['price']} (Quantity: ${foundProduct['quantity']})');
} else {
print('Product not found');
}
}
// TODO: Complete this function to find a product by name
Map<String, dynamic>? findProduct(List<Map<String, dynamic>> cart, String productName) {
// TODO: Loop through the cart and return the first product that matches the productName
// TODO: Return null if no matching product is found
}All lessons in Fundamentals
4Operators Part 2
Comparison OperatorsLogical ANDLogical ORLogical NOTType Test OperatorsRecap - Making Comparisons7Working with Strings
String ConcatenationString InterpolationMulti-line StringsString PropertiesBasic String Methods10Collections - Maps Basics
What are Maps?Creating a MapAccessing Values by KeyKey-Value PairsGetting Map SizeGetting KeysGetting ValuesChecking if a Key Exists13Null Safety In Depth
Understanding NullNullable TypesNon-Nullable TypesNull Assertion OperatorLate InitializationRecap - Handling Null Safely16Fundamentals Challenges
Challenge: List of calculationChallenge: Sum of numbersChallenge: Find product2Variables and Basic Data Types
What are Variables?StringsIntegers (int)Doubles (double)Booleans (bool)Type Inference with 'var'Final VariablesConstant VariablesNaming ConventionsBasic Null SafetyRecap - Declaring Variables8Control Flow - Loops
The 'for' LoopThe 'while' LoopThe 'do-while' LoopUsing 'break' in LoopsUsing 'continue' in LoopsRecap - Repeating Code3Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorIncrement and DecrementAssignment ShortcutsRecap - Simple Calculations6Control Flow - Decision Making
The 'if' StatementThe 'else' StatementThe 'else if' StatementRecap - Simple DecisionsNested 'if' StatementsThe 'switch' Statement9Collections - Lists Basics
What are Lists?Creating a ListAccessing by IndexGetting List LengthAdding ElementsRemoving ElementsChecking if a List is EmptyIterating Over a List12Functions Advanced
Optional Positional ParametersNamed ParametersRequired Named ParametersDefault Parameter ValuesRecap - Function Parameters15Project: Simple Calculator
Setting UpDeclaring Number