mirror of
https://github.com/flutter/samples.git
synced 2026-04-29 11:06:34 +00:00
refactor Result class, remove asOk and asError (#2542)
As discussed in the PR for the Result pattern implementation (https://github.com/flutter/website/pull/11444) @parlough recommended that `asError` and `asOk` should be not be used, and instead we should use proper exhaustiveness checking. This PR removes the two "convenience" methods and refactors code. In some cases, it was enough with writing a proper `if` clause, while in others it was necessary to use a `switch`. Still, they are present in the `testing` folder, as they can be useful for testing purposes. ## Pre-launch Checklist - [x] I read the [Flutter Style Guide] _recently_, and have followed its advice. - [x] I signed the [CLA]. - [x] I read the [Contributors Guide]. - [x] I have added sample code updates to the [changelog]. - [x] I updated/added relevant documentation (doc comments with `///`). If you need help, consider asking for advice on the #hackers-devrel channel on [Discord]. <!-- Links --> [Flutter Style Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md [CLA]: https://cla.developers.google.com/ [Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md [Contributors Guide]: https://github.com/flutter/samples/blob/main/CONTRIBUTING.md [changelog]: ../CHANGELOG.md
This commit is contained in:
@@ -40,11 +40,13 @@ class BookingCreateUseCase {
|
||||
}
|
||||
final destinationResult =
|
||||
await _fetchDestination(itineraryConfig.destination!);
|
||||
if (destinationResult is Error<Destination>) {
|
||||
_log.warning('Error fetching destination: ${destinationResult.error}');
|
||||
return Result.error(destinationResult.error);
|
||||
switch (destinationResult) {
|
||||
case Ok<Destination>():
|
||||
_log.fine('Destination loaded: ${destinationResult.value.ref}');
|
||||
case Error<Destination>():
|
||||
_log.warning('Error fetching destination: ${destinationResult.error}');
|
||||
return Result.error(destinationResult.error);
|
||||
}
|
||||
_log.fine('Destination loaded: ${destinationResult.asOk.value.ref}');
|
||||
|
||||
// Get Activity objects from repository
|
||||
if (itineraryConfig.activities.isEmpty) {
|
||||
@@ -54,11 +56,13 @@ class BookingCreateUseCase {
|
||||
final activitiesResult = await _activityRepository.getByDestination(
|
||||
itineraryConfig.destination!,
|
||||
);
|
||||
if (activitiesResult is Error<List<Activity>>) {
|
||||
_log.warning('Error fetching activities: ${activitiesResult.error}');
|
||||
return Result.error(activitiesResult.error);
|
||||
switch (activitiesResult) {
|
||||
case Error<List<Activity>>():
|
||||
_log.warning('Error fetching activities: ${activitiesResult.error}');
|
||||
return Result.error(activitiesResult.error);
|
||||
case Ok<List<Activity>>():
|
||||
}
|
||||
final activities = activitiesResult.asOk.value
|
||||
final activities = activitiesResult.value
|
||||
.where(
|
||||
(activity) => itineraryConfig.activities.contains(activity.ref),
|
||||
)
|
||||
@@ -74,7 +78,7 @@ class BookingCreateUseCase {
|
||||
final booking = Booking(
|
||||
startDate: itineraryConfig.startDate!,
|
||||
endDate: itineraryConfig.endDate!,
|
||||
destination: destinationResult.asOk.value,
|
||||
destination: destinationResult.value,
|
||||
activity: activities,
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user