mirror of
https://github.com/flutter/samples.git
synced 2025-11-14 11:28:36 +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:
@@ -42,18 +42,22 @@ class BookingRepositoryRemote implements BookingRepository {
|
||||
try {
|
||||
// Get booking by ID from server
|
||||
final resultBooking = await _apiClient.getBooking(id);
|
||||
if (resultBooking is Error<BookingApiModel>) {
|
||||
return Result.error(resultBooking.error);
|
||||
switch (resultBooking) {
|
||||
case Error<BookingApiModel>():
|
||||
return Result.error(resultBooking.error);
|
||||
case Ok<BookingApiModel>():
|
||||
}
|
||||
final booking = resultBooking.asOk.value;
|
||||
final booking = resultBooking.value;
|
||||
|
||||
// Load destinations if not loaded yet
|
||||
if (_cachedDestinations == null) {
|
||||
final resultDestination = await _apiClient.getDestinations();
|
||||
if (resultDestination is Error<List<Destination>>) {
|
||||
return Result.error(resultDestination.error);
|
||||
switch (resultDestination) {
|
||||
case Error<List<Destination>>():
|
||||
return Result.error(resultDestination.error);
|
||||
case Ok<List<Destination>>():
|
||||
}
|
||||
_cachedDestinations = resultDestination.asOk.value;
|
||||
_cachedDestinations = resultDestination.value;
|
||||
}
|
||||
|
||||
// Get destination for booking
|
||||
@@ -62,11 +66,12 @@ class BookingRepositoryRemote implements BookingRepository {
|
||||
|
||||
final resultActivities =
|
||||
await _apiClient.getActivityByDestination(destination.ref);
|
||||
|
||||
if (resultActivities is Error<List<Activity>>) {
|
||||
return Result.error(resultActivities.error);
|
||||
switch (resultActivities) {
|
||||
case Error<List<Activity>>():
|
||||
return Result.error(resultActivities.error);
|
||||
case Ok<List<Activity>>():
|
||||
}
|
||||
final activities = resultActivities.asOk.value
|
||||
final activities = resultActivities.value
|
||||
.where((activity) => booking.activitiesRef.contains(activity.ref))
|
||||
.toList();
|
||||
|
||||
@@ -88,20 +93,22 @@ class BookingRepositoryRemote implements BookingRepository {
|
||||
Future<Result<List<BookingSummary>>> getBookingsList() async {
|
||||
try {
|
||||
final result = await _apiClient.getBookings();
|
||||
if (result is Error<List<BookingApiModel>>) {
|
||||
return Result.error(result.error);
|
||||
switch (result) {
|
||||
case Ok<List<BookingApiModel>>():
|
||||
final bookingsApi = result.value;
|
||||
return Result.ok(bookingsApi
|
||||
.map(
|
||||
(bookingApi) => BookingSummary(
|
||||
id: bookingApi.id!,
|
||||
name: bookingApi.name,
|
||||
startDate: bookingApi.startDate,
|
||||
endDate: bookingApi.endDate,
|
||||
),
|
||||
)
|
||||
.toList());
|
||||
case Error<List<BookingApiModel>>():
|
||||
return Result.error(result.error);
|
||||
}
|
||||
final bookingsApi = result.asOk.value;
|
||||
return Result.ok(bookingsApi
|
||||
.map(
|
||||
(bookingApi) => BookingSummary(
|
||||
id: bookingApi.id!,
|
||||
name: bookingApi.name,
|
||||
startDate: bookingApi.startDate,
|
||||
endDate: bookingApi.endDate,
|
||||
),
|
||||
)
|
||||
.toList());
|
||||
} on Exception catch (e) {
|
||||
return Result.error(e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user