Let's start building an n-tier architecture, starting with the controller layer:
- Since our controllers are supposed to represent a separate tier in the app, it's appropriate to put them in their own folder. Inside the lib directory, create a new folder called controllers.
- Now, we can create a new dart file, plan_controller.dart. This class will be responsible for all the business logic in our app. Let's start with the class declaration:
import '../models/data_layer.dart';
class PlanController {
final _plans = <Plan>[];
// This public getter cannot be modified by any other object
List<Plan> get plans => List.unmodifiable(_plans);
}
- Now, let's add the methods that will be responsible for creating and deleting plans. This is also an appropriate location where we can apply some business logic.
First, create a private method that will check a list of items and search for duplicate names. If it finds any...