35
loading...
This website collects cookies to deliver better user experience
class UpdateFarmerCommand extends BaseCommand {
UpdateFarmerCommand(BuildContext c) : super(c);
/// Calls FarmerService.updateFarmer method
///
/// Recieves farmer data and buildcontext from widget and pass it to the farmerService.updateFarmer and fileService.uploadFarmerProfilePicture method.
Future<bool> run({
required FarmerServiceModel farmerServiceModel,
required File? farmerProfilePicture,
required BuildContext context,
}) async {
bool farmerAddedSuccess = false;
if (farmerProfilePicture != null) {
final farmerProfilePictureUrl =
await fileservice.uploadFarmerProfilePicture(farmerProfilePicture);
farmerServiceModel.saveProfilePicture(farmerProfilePictureUrl);
}
await farmerService
.updateFarmer(farmerServiceModel: farmerServiceModel)
.then((value) => farmerAddedSuccess = true);
return farmerAddedSuccess;
}
}
FarmerServiceModel farmerServiceModel
instance, an optional File? farmerProfilePicture
and a BuildContext context
. If the farmerProfilePicture
is not null upload it to cloud storage and save the url. Call the farmerService.updateFarmer
method.class FarmerService {
final farmerRef =
FirebaseFirestore.instance.collection('farmers').withConverter(
fromFirestore: (snapshot, _) =>
FarmerServiceModel.fromJson(snapshot.data()!),
toFirestore: (farmerModel, _) => farmerModel.toJson(),
);
Future<void> updateFarmer({
required FarmerServiceModel farmerServiceModel,
}) async {
return farmerRef
.doc(farmerServiceModel.id)
.update(farmerServiceModel.toJson())
.then((value) => value)
.catchError((error) {
print(error.toString());
});
}
}
FarmerServiceModel farmerServiceModel
instance. The .doc()
and .update()
method is called on farmerRef()
where the farmerServiceModel.id
and the farmerServiceModel.toJson()
is passed the .doc()
and .update()
respectively.farmerServiceModel.toJson()
method simply convert the farmerServiceModel
properties to a Map.35