35
loading...
This website collects cookies to deliver better user experience
FarmerService()
class and the AddFarmerCommand()
class. These command is responsible for the S and C respectively for the MCV + S architecture. S for service and C for controller.FarmerService()
class is responsible for making all network request to cloud firestore Farmers collection and Farmer Document. These request include the standard CRUD related (Create, Read, Update, Delete) request. This class is the bridge between the application and cloud firestore Farmers collection.FarmerService()
class.class FarmerService {
final farmerRef =
FirebaseFirestore.instance.collection('farmers').withConverter(
fromFirestore: (snapshot, _) =>
FarmerServiceModel.fromJson(snapshot.data()!),
toFirestore: (farmerModel, _) => farmerModel.toJson(),
);
...
}
farmerRef
is the main variable in the FarmerService()
class. It stores a Collection Reference to the Farmers collection. The FarmerServiceModel that was created on Day 1 is used thanks to the withConverter()
method. The combination of the two make the farmer document type safe when the CRUD network request are made. You can read more about this hereclass FarmerService {
...
Future<DocumentReference<FarmerServiceModel>> addFarmer({
required FarmerServiceModel farmerServiceModel,
}) async {
return farmerRef
.add(farmerServiceModel)
.then((value) => value)
.catchError((error) {
print(error.toString());
});
}
}
AddFarmer()
method receive a FarmerServiceModel()
instance from the AddFarmer().run()
command (more on this later) and call the farmerRef.add(farmerServiceModel)
method.farmerServiceModel
data to cloud firestore farmers collection as a Document.class FarmerService {
final farmerRef =
FirebaseFirestore.instance.collection('farmers').withConverter(
fromFirestore: (snapshot, _) =>
FarmerServiceModel.fromJson(snapshot.data()!),
toFirestore: (farmerModel, _) => farmerModel.toJson(),
);
Future<DocumentReference<FarmerServiceModel>> addFarmer({
required FarmerServiceModel farmerServiceModel,
}) async {
return farmerRef
.add(farmerServiceModel)
.then((value) => value)
.catchError((error) {
print(error.toString());
});
}
}
withConverter
method It only takes 18 lines of code to create a Farmer Document in cloud firestore./// This class is responsible for farmer registration.
class AddFarmerCommand extends BaseCommand {
AddFarmerCommand(BuildContext c) : super(c);
/// Calls FarmerService.addFarmer method
///
/// Recieves farmer data and buildcontext from widget and pass it to the farmerService.addFarmer method.
Future<bool> run({
required FarmerServiceModel farmerServiceModel,
required BuildContext context,
}) async {
bool farmerAddedSuccess = false;
await farmerService
.addFarmer(farmerServiceModel: farmerServiceModel)
.then((value) => farmerAddedSuccess = true);
return farmerAddedSuccess;
}
}
run()
. This method will pass an instance of the FarmerServiceModel()
to the FarmerService().addFarmer
method.AddFarmerScreen()
widget (not created as yet) job is to focus on layout and populating an instance of the FarmerServiceModel
with user input. In the next post, I will be showing you how these two job will be separated into the LayoutView and the Controller.AddFarmercommand().run()
only job is to pass an instance of the FarmerServiceModel()
to the FarmerService.addFarmer()
method. The instance is received from the AddFarmerScreen()
controller.FarmerService.addFarmer()
method will make the network request to cloud firestore where the FarmerServiceModel()
instance will be stored as a Farmers collection document.35