...

/

Solution: Dependency Injection

Solution: Dependency Injection

Explore solutions to the dependency injection challenges.

Solutions

Compare your solutions with the ones provided below.

name: ecommerce_app
description: A new Flutter application.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
# SOLUTION-1: Import the get dependency
get: ^4.6.5
dev_dependencies:
flutter_lints: ^1.0.4
flutter:
uses-material-design: true

Challenge 1: Add the Get package

In pubspec.yaml file, we import the Get package into your application for state management (line 13).

Challenge 2: Create the CartController class

In the lib/state/cart_controller.dart file, we create a state object class called CartController with a list of CartItem objects. The addItem() and removeItem() functions allow us to add and remove items to and from the list, respectively.

The class also has getters to get the total number of items in the cart and the total checkout price of the cart named itemCount and totalPrice, respectively.

Challenge 3: Provide the CartController to the Get library dependency

...